473,471 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 16 '05 #1
1 3888
"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 16 '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: 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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.