M
Michael Russell
Hi,
I'm having trouble retrieving matches from strings using regular
expressions. I'm parsing a string that's a date with either a 2 or 4
digit year (ex. "1/15/2005"). The RegExp object is created as such:
m_rx = new Regex( @"^(\d{1,2})/(\d{1,2})/(\d{2,4})$",
RegexOptions.IgnoreCase );
What I want to be able to do is retrieve separately the day, month, and
year. When I do "m_rx.Matches( text )" for the above date, I get a
Match. However, I only get one Match, and that match only has 1 Group,
which in turn only has one item in its CaptureCollection. To verify
this, I tried the following:
Group g = matches[ 0 ];
CaptureCollection cc = g.Captures;
Console.WriteLine( "Num matches: {0}", matches.Count );
Console.WriteLine( "Num Collections: {0}", g.Captures.Count );
Console.WriteLine( "Num items: {0}, {1}", cc.Count, cc[ 0 ].Value );
I expected to be able to access each capture by using something like
"cc[0].Value".
The first item in each (Match, Group, Capture) is always the original
string, as I would expect. So my question becomes: Is my pattern
wrong, or am I looking in the wrong place for the values?
Thanks,
Michael
I'm having trouble retrieving matches from strings using regular
expressions. I'm parsing a string that's a date with either a 2 or 4
digit year (ex. "1/15/2005"). The RegExp object is created as such:
m_rx = new Regex( @"^(\d{1,2})/(\d{1,2})/(\d{2,4})$",
RegexOptions.IgnoreCase );
What I want to be able to do is retrieve separately the day, month, and
year. When I do "m_rx.Matches( text )" for the above date, I get a
Match. However, I only get one Match, and that match only has 1 Group,
which in turn only has one item in its CaptureCollection. To verify
this, I tried the following:
Group g = matches[ 0 ];
CaptureCollection cc = g.Captures;
Console.WriteLine( "Num matches: {0}", matches.Count );
Console.WriteLine( "Num Collections: {0}", g.Captures.Count );
Console.WriteLine( "Num items: {0}, {1}", cc.Count, cc[ 0 ].Value );
I expected to be able to access each capture by using something like
"cc[0].Value".
The first item in each (Match, Group, Capture) is always the original
string, as I would expect. So my question becomes: Is my pattern
wrong, or am I looking in the wrong place for the values?
Thanks,
Michael