quick question on RegEx

  • Thread starter Thread starter K. Shier
  • Start date Start date
K

K. Shier

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?

more importantly, how can you refer to a Group by name in code?
 
You can access the names of groups in a Regex through the GetGroupNames
function of the Regex object:

Dim r as New Regex("\b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b")
Dim GroupNames as String() = r.GetGroupNames


Accessing groups by name is accomplished like this:

Dim r as New Regex("\b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b")
Dim m as Match = r.Match("Today is 08/26/2003.")
Dim y as String = m.Groups("year").Value


Brian Davis
www.knowdotnet.com
 
Back
Top