none
Regular expression mystery RRS feed

  • Question

  • Hi!

    The first code block does not give me a match.

    string pattern = @"([0-9]*)";
    var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
    Match match = regex.Match(@"<p align=""right"">325</p>");

    This one will though but I don't understand why only one of them work?

    string pattern = @"([0-9]*,[0-9]*)";
    var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
    Match match = regex.Match(@"<p align=""right"">32,5</p>");
    /Måns
    Tuesday, February 19, 2013 7:16 AM

Answers

  • Hi,

    the core problem with your first regex is, that the number of [0-9] can be zero. So you get lots of matches (and the first match is an empty match).

    You can see that behaviour if you change your code like this:

                string pattern = @"([0-9]{1})";
                var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
                var matches = regex.Matches(@"<p align=""right"">325</p>");
                foreach (var m in matches)
                {
                    Console.WriteLine(m.ToString());
                }

    (Just run it in the debugger so you can have a look at matches and at each single hit! Also check out the description of Match at http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx!)

    Does this help already or do you need further help?

    With kind regards,

    Konrad

    Tuesday, February 19, 2013 12:45 PM

All replies

  • Can you post a sample of the data you are using?  Also what is your default international settting?

    The first code has 325 and the second 32,5.  Is this correct?


    jdweng

    Tuesday, February 19, 2013 11:54 AM
  • Yes, it is correct. If you run the code snippets as they are the second will match "32,5" but the first will not match 325. And, that is what is confusing me. I would have thought that both would match their respective numbers. That is, I expected codesnippet number one to match "325" and snippet two to match "32,5". But, only the second snippet matched according to my expectations.

    /Måns

    Tuesday, February 19, 2013 12:42 PM
  • Hi,

    the core problem with your first regex is, that the number of [0-9] can be zero. So you get lots of matches (and the first match is an empty match).

    You can see that behaviour if you change your code like this:

                string pattern = @"([0-9]{1})";
                var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
                var matches = regex.Matches(@"<p align=""right"">325</p>");
                foreach (var m in matches)
                {
                    Console.WriteLine(m.ToString());
                }

    (Just run it in the debugger so you can have a look at matches and at each single hit! Also check out the description of Match at http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx!)

    Does this help already or do you need further help?

    With kind regards,

    Konrad

    Tuesday, February 19, 2013 12:45 PM