0

How to get the numeric value from the following string using regular expression

   AD  .6547 BNM

The result should be

 .6547
2
  • Regex EX = new Regex("(<Z>[0-9.]*)"); Match M = EX.Match(" AD .6547 BNM "); String str = M.Groups["Z"].Value; But its not working Commented Dec 16, 2010 at 6:57
  • 1
    Can there also be values like 1.234, 1.2E34, -0.123, 123,456.789 or 123.? Commented Dec 16, 2010 at 7:10

4 Answers 4

3

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);
    }
3
var src = "AD  .6547 BNM";

var match = Regex.Match(input, @"(\.\d+)");
if (match.Success) {
    var result = match.Groups[1].Value;
}
3
  • @RageZ: yep. But it still strongly typed, since on the compilation stage csc.exe decides what actual type the variable is. So in IL you get the specific type.
    – zerkms
    Commented Dec 16, 2010 at 7:06
  • @RageZ: yep, it is a keyword for lazy developers which also makes sources more readable: var dict = new Dictionary<string, Some.Namespace.Some.Class>(); vs Dictionary<string, Some.Namespace.Some.Class> dict = new Dictionary<string, Some.Namespace.Some.Class>(); ;-)
    – zerkms
    Commented Dec 16, 2010 at 7:09
  • I don't do c# anymore but yes I remember those templated stuff was really painful.
    – RageZ
    Commented Dec 16, 2010 at 7:13
2
\.\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;
1

try

\.\d*

it will select "." and all digits after it.

1
  • it will match also any dot foobar . baz -- like this
    – zerkms
    Commented Dec 16, 2010 at 7:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.