How to get the numeric value from the following string using regular expression
AD .6547 BNM
The result should be
.6547
I would generally reach for Regex.Match to start with. You will get a slight benefit by instantiating a Regex object and using the Matches instance method rather than using the static Match method if you have multiple strings to parse. It's only a small benefit but hey...
If you have significant performance concerns and the format of your input strings are static you may even want to consider abandoning regex's in favour of string.Substring or even string.Split. Your question prompted me to measure the performance differences between a few different methods, code below.
static void TestParse()
{
List<string> strList = new List<string>
{
"AD .1234 BNM",
"AD .6547 BNM",
"AD .6557 BNM",
"AD .6567 BNM",
"AD .6577 BNM",
"AD .6587 BNM",
"AD .6597 BNM",
"AD .6540 BNM",
"AD .6541 BNM",
"AD .6542 BNM"
};
Stopwatch stopwatch = new Stopwatch();
string result = "";
stopwatch.Start();
for (int i=0; i<100000; i++)
foreach (string str in strList)
{
var match = Regex.Match(str, @"(\.\d+)");
if (match.Success)
result = match.Groups[1].Value;
}
stopwatch.Stop();
Console.WriteLine("\nTotal Regex.Match time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);
result = "";
stopwatch.Reset();
stopwatch.Start();
Regex exp = new Regex(@"(\.\d+)", RegexOptions.IgnoreCase);
for (int i = 0; i < 100000; i++)
foreach (string str in strList)
result = exp.Matches(str)[0].Value;
stopwatch.Stop();
Console.WriteLine("Total Regex object time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);
result = "";
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < 100000; i++)
foreach (string str in strList)
result = str.Substring(4, 5);
stopwatch.Stop();
Console.WriteLine("Total string.Substring time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);
result = "";
stopwatch.Reset();
stopwatch.Start();
char[] seps = { ' ' };
for (int i = 0; i < 100000; i++)
foreach (string str in strList)
result = str.Split(seps, StringSplitOptions.RemoveEmptyEntries)[1];
stopwatch.Stop();
Console.WriteLine("Total string.Split time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);
}
var src = "AD .6547 BNM";
var match = Regex.Match(input, @"(\.\d+)");
if (match.Success) {
var result = match.Groups[1].Value;
}
csc.exe
decides what actual type the variable is. So in IL you get the specific type.
var dict = new Dictionary<string, Some.Namespace.Some.Class>();
vs Dictionary<string, Some.Namespace.Some.Class> dict = new Dictionary<string, Some.Namespace.Some.Class>();
;-)
\.\d+
should do it and some c# code:
Regex exp = new Regex(
@"(\.\d+)",
RegexOptions.IgnoreCase);
string InputText = "AD .1234";
MatchCollection MatchList = exp.Matches(InputText);
Match FirstMatch = MatchList[0];
string value = MatchList[0].value;
try
\.\d*
it will select "." and all digits after it.
Regex EX = new Regex("(<Z>[0-9.]*)"); Match M = EX.Match(" AD .6547 BNM "); String str = M.Groups["Z"].Value;
But its not working1.234
,1.2E34
,-0.123
,123,456.789
or123.
?