CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    440

    Question Regular expressions

    The code below...
    Code:
    string sInput = @"from 2010 to 2011";
    Regex reg1 = new Regex(@"\d+");
    Regex reg2 = new Regex(@"(\d+)");
    Match m = reg1.Match(sInput);
                
    Console.WriteLine(sInput);
    Console.WriteLine("Regex 1 groups ...");
    foreach (Group g in m.Groups)
    {
        Console.WriteLine(g.ToString());
    }
    
    m = reg2.Match(sInput);
    Console.WriteLine("Regex 2 groups ...");
    foreach (Group g in m.Groups)
    {
        Console.WriteLine(g.ToString());
    }
    Gives me following result
    Code:
    from 2010 to 2011
    Regex 1 groups ...
    2010
    Regex 2 groups ...
    2010
    2010
    Can anyone explain to me why for the first regex I'm only getting 2010 and why for the second regex I'm getting 2010 twice ?

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Regular expressions

    I'm not completely sure, but I think the answer is as follows:

    When you use the parenthesis you are defining a 'subexpression'. For example (as on MSDN) a telephone number regex might look like (\d\d\d)-(\d\d\d\d) which has two sub-expressions (the first 3 digits and the last 4 digits). I think when you access Match.Groups it returns the first match and all subexpressions thereof. Thus it returns 2010 the first match and then 2010 again as the (only) subexpression.

    For example the output of:

    Code:
    public static void Main(string[] args)
    {
        Regex phone = new Regex(@"(\d\d\d)-(\d\d\d\d)");
        string test = "the number is 123-4567 so call it";
        
        Match m = phone.Match(test);
        foreach(Group g in m.Groups)
        {
            Console.WriteLine(g.ToString());
        }
        
        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
    }
    is:

    Code:
    123-4567
    123
    4567
    The relevant MSDN page is: http://msdn.microsoft.com/en-us/libr...ch.groups.aspx
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    440

    Re: Regular expressions

    Ok, so basicaly the Groups property returns the first match and all parenthesized subexpressions of this first match.

    My approach was incorrect ! I should have used the Regex.Matches property instead ...
    Code:
    string sInput = @"from 2010 to 2011";
    Regex reg = new Regex(@"\d+");
    MatchCollection matches = reg.Matches(sInput);
                
    Console.WriteLine(sInput);
    Console.WriteLine("Regex matches ...");
    foreach (Match m in matches)
    {
        Console.WriteLine(m.ToString());
    }
    Which results in ...
    Code:
    from 2010 to 2011
    Regex matches ...
    2010
    2011
    Thx for your support

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured