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 2 1268
Michael Russell wrote: So my question becomes: Is my pattern wrong, or am I looking in the wrong place for the values?
I only added "named groups" to your pattern and it seems to work:
string pattern =@"^(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})$";
Regex re = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = re.Match("1/15/2005");
if (m.Success) {
int day = int.Parse(m.Groups["day"].Value);
int month = int.Parse(m.Groups["month"].Value);
int year = int.Parse(m.Groups["year"].Value);
Trace.WriteLine(day + ", " + month + ", " + year);
}
hth,
Max
Markus Stoeger wrote: Michael Russell wrote:
So my question becomes: Is my pattern wrong, or am I looking in the wrong place for the values?
I only added "named groups" to your pattern and it seems to work:
string pattern =@"^(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})$"; Regex re = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = re.Match("1/15/2005");
if (m.Success) { int day = int.Parse(m.Groups["day"].Value); int month = int.Parse(m.Groups["month"].Value); int year = int.Parse(m.Groups["year"].Value);
Trace.WriteLine(day + ", " + month + ", " + year); }
hth, Max
Thanks Max, turns out I was expecting (wrongly) multiple matches,
instead of a single match with multiple captures. I adjusted to match
your code (without the named groups) and it worked perfectly.
Michael This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mark Downes |
last post by:
I need to split a string based on a character, such as a space (0x20).
The caveat is that I need to ignore the character if it is found
between a pair of some other characters, such as...
|
by: Mark Downes |
last post by:
I need to split a string based on a character, such as a space (0x20).
The caveat is that I need to ignore the character if it is found
between a pair of some other characters, such as...
|
by: rufus |
last post by:
I need to parse some HTML and add links to some keywords (up to 1000) defined
in a DB table. What I need to do is search for these keywords and if they
are not already a link, and they are not...
|
by: shawnmkramer |
last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just
hanging and eventually getting a message "Requested Service not
found"?
I have the following pattern:
^(?<OrgCity>(+)+),...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |