473,408 Members | 2,405 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,408 software developers and data experts.

Regex question: Retrieve group names in code?

[Pardon the crossposting, but it seemed appropriate given the lack of a
dedicated group]

Is there any way to retrieve the name of a capture group from the classes
provided in the RegularExpressions namespace? GroupCollection implements
ICollection and IEnumerable, not IDictionary, so there doesn't seem to be
any means to retrieve key values.

While I realize this isn't generally needed in a production scenario (you
normally KNOW what your groups are called), it is quite necessary if you're
building a general regular expression evaluation app (like The Regulator or
Regex Workbench).

In case I'm not being clear, consider the following example (error-trapping
and most braces omited for brevity--and in two languages to justify the
crosspost):

[You have a form with three text boxes, txtRegex, txtString, and txtOutput.
The user puts "(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{4})" into
txtRegex and "Today's date is 12/1/2004. Where has the year gone?" in
txtString and clicks the Evaluate button.]

-------VB--------
Private Sub btnEval_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnEval.Click
Dim rgx As Regex, m As Match
rgx = New Regex(txtRegex.Text)
m = rgx.Match(txtString.Text)
If m.Success Then
For Each g As Group In m.Groups
If g.Index <> 0 Then
' This is what I'd LIKE to do
txtOutput.AppendText("Group '" & g.Name & "' = '" & g.Value
& "'" & Environment.NewLine)
End If
Next
End If
End Sub
--------------------

--------C#--------
private void btnEval_Click(object sender, System.EventArgs e)
{
Regex rgx = new Regex(txtRegex.Text);
Match m = rgx.Match(txtString.Text);
if (m.Success)
foreach (Group g in m.Groups)
if (g.Index != 0)
// This is what I'd LIKE to do
txtOutput.AppendText("Group '" + g.Name + "' = '" + g.Value
+ "'\r\n");
}
--------------------

I would then like the output text box to contain:

Group 'month' = '12'
Group 'day' = '1'
Group 'year' = '2004'

(And pardon my C# if I made any syntax or style mistakes.)
Jul 21 '05 #1
1 1294
"Jeff Johnson [MVP: VB]" <i.***@enough.spam> wrote in news:usM8pp91EHA.4028
@TK2MSFTNGP15.phx.gbl:
I would then like the output text box to contain:

Group 'month' = '12'
Group 'day' = '1'
Group 'year' = '2004'


You can use the method: GroupNameFromNumber to retreive the group name.
To get the GroupNumber, you can just iterate through the group collection.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Jul 21 '05 #2

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

Similar topics

2
by: Jose | last post by:
There's something for me to learn with this example, i'm sure :) Given this text: "....." and my first attempt at capture the groups: "(?:\)" RegExTest gives me what i expect: 6 captured...
1
by: Jeff Johnson [MVP: VB] | last post by:
Is there any way to retrieve the name of a capture group from the classes provided in the RegularExpressions namespace? GroupCollection implements ICollection and IEnumerable, not IDictionary, so...
8
by: Richard Lionheart | last post by:
Hi All, I tried using RegEx, but the compiler barfed with "The type of namespace 'RegEx' could not be found. Prior to this, I had the same problem with MatchCollection, but discovered it's...
1
by: K. Shier | last post by:
while writing a RegEx, you implicily name your Groups: \b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b so, why isn't .Name a property of a System.Text.RegularExpressions.RegEx.Group? ...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
1
by: TtfnJohn | last post by:
I have two small scripts that while on the surface should both work the problem is they don't. Here's the first one: import re testString = 'Thap,fpvi,*!wtyd@*.dip.t-dialin.net:*!ylx@*.dip.t-...
4
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of...
3
by: mathieu | last post by:
I do not understand what is wrong with the following regex expression. I clearly mark that the separator in between group 3 and group 4 should contain at least 2 white space, but group 3 is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.