How do know the name of the capture in Regex

M

Mark Coffman

Lets say I don't know the pattern string ahead of time (ie. it might be
entered in a form, not hardcoded like below). How can i get the name of the
matched groups (in this case "pagetitle")?



string pattern = @"<title>(?<pagetitle>.*?)</title>" ;

Regex regex = new Regex(
pattern,
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);

MatchCollection found = regex.Matches(html);
string outXML = "" ;

foreach ( Match aMatch in found )
{
for ( int x=1; x<aMatch.Groups.Count ; x++ )
{
outXML += aMatch.Groups["idontknowthis"].Value + "\n" ;
}
}
 
B

BMermuys

Hi,
[inline]

Mark Coffman said:
Lets say I don't know the pattern string ahead of time (ie. it might be
entered in a form, not hardcoded like below). How can i get the name of the
matched groups (in this case "pagetitle")?



string pattern = @"<title>(?<pagetitle>.*?)</title>" ;

Regex regex = new Regex(
pattern,
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);

MatchCollection found = regex.Matches(html);
string outXML = "" ;

foreach ( Match aMatch in found )
{
for ( int x=1; x<aMatch.Groups.Count ; x++ )
{
outXML += aMatch.Groups["idontknowthis"].Value + "\n" ;
}
}

If you want to enumerate, you can just use an integer index. If you want
the names of the groups you can use Regex.GetGroupNames(). See the
following:

....
....execute regex...
....
string[] names = regex.GetGroupNames();
for (int x = 1; x<aMatch.Groups.Count: ++x)
{
Console.Writeline ("Name={0} Value={0}", names[x],
aMatch.Groups[x].Value);
}
....
....

HtH
Greetings
 
M

Mythran

Mark Coffman said:
Lets say I don't know the pattern string ahead of time (ie. it might be
entered in a form, not hardcoded like below). How can i get the name of the
matched groups (in this case "pagetitle")?



string pattern = @"<title>(?<pagetitle>.*?)</title>" ;

Regex regex = new Regex(
pattern,
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);

MatchCollection found = regex.Matches(html);
string outXML = "" ;

foreach ( Match aMatch in found )
{
for ( int x=1; x<aMatch.Groups.Count ; x++ )
{
outXML += aMatch.Groups["idontknowthis"].Value + "\n" ;
}
}

Hmm....

Not only do they have names (for named groups) but they also have indexes :)

Each group has an index. So aMatch.Groups(0) is a group (always the first
match), then aMatch.Groups(1), et cetera.

Mythran
 
M

mikeb

Mark said:
Lets say I don't know the pattern string ahead of time (ie. it might be
entered in a form, not hardcoded like below). How can i get the name of the
matched groups (in this case "pagetitle")?



string pattern = @"<title>(?<pagetitle>.*?)</title>" ;

Regex regex = new Regex(
pattern,
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);

MatchCollection found = regex.Matches(html);
string outXML = "" ;

foreach ( Match aMatch in found )
{
for ( int x=1; x<aMatch.Groups.Count ; x++ )
{
outXML += aMatch.Groups["idontknowthis"].Value + "\n" ;
}
}

Use regex.GetGroupNames(), but be aware that it also returns the unnamed
groups with numbers for names:

//--------------------------
foreach ( Match aMatch in found ) {
foreach (string name in regex.GetGroupNames()) {
Console.WriteLine( @"Group[""{0}""] = ""{1}""",
name,
aMatch.Groups[name]);
}
}
//--------------------------
 
M

Mark Coffman

Exactly what I was looking for. Also thank you to mikeb for a working
solution. I don't know how I missed this before.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top