473,386 Members | 1,795 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,386 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 1290
"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...

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.