-
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 ?
-
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:
The relevant MSDN page is: http://msdn.microsoft.com/en-us/libr...ch.groups.aspx
-
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 :thumb: