473,327 Members | 2,012 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,327 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.)
Nov 21 '05 #1
1 3915
"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/
Nov 21 '05 #2

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

Similar topics

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...
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...
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: George Durzi | last post by:
Consider this excerpt from some HTML. (This is a copy from View->Source, except for the comment) <TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 border=0> <?xml version="1.0" encoding="UTF-16"?>...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.