473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regular expression groupings/collections

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
Jun 1 '06 #1
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

Jun 1 '06 #2
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
Jun 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
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...
9
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...
4
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...
5
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>(+)+),...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.