WTF Next?

Dev ramblings from a master of nothing.

  Home  |   Contact  |   Syndication    |   Login
  135 Posts | 0 Stories | 103 Comments | 0 Trackbacks

News

INETA Community Speakers Program
GeeksWithBlogs.net: WTFNext's hosting!

View Stacy Vicknair's profile on LinkedIn

Twitter







Tag Cloud


Archives

Post Categories

Community Links

User Groups

I'm currently studying for my first Microsoft certification exam, the 070-536, and during the process I came across this small quirk. Although most object arrays or collections in VB.NET start at 0, the Match.Groups collection starts at 1. This could potentially be a small frustration for those who don't use regex that often (especially with the added need to extract the information instead of just validating it).

So, if you are extracting information using regex in the future, keep in mind your index starts at 1 for Match.Groups. If possible, avoid the index by using for-each enumeration. Here's an illustration of the point from the MSDN article on Match.Groups, importing the System.Text.RegularExpressions namespace:

Dim text As String = "One car red car blue car"
Dim pattern As String = "(\w+)\s+(car)"

' Instantiate the regular expression object.
Dim r As Regex = New Regex(pattern, RegexOptions.IgnoreCase)

' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
Dim matchcount As Integer = 0
Do While m.Success
    matchcount += 1
    Console.WriteLine("Match" & (matchcount))
    Dim i As Integer
    For i = 1 To 2
        Dim g As Group = m.Groups(i)
        Console.WriteLine("Group" & i & "='" & g.ToString() & "'")
        Dim cc As CaptureCollection = g.Captures
        Dim j As Integer
        For j = 0 To cc.Count - 1
            Dim c As Capture = cc(j)
            Console.WriteLine("Capture" & j & "='" & c.ToString() _
               & "', Position=" & c.Index)
        Next
    Next
    m = m.NextMatch()
Loop

It is very subtle and hardly even mentioned in the MSDN article, but you'll see above that the For loop initializes i as 1, not 0.

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.match.groups.aspx

 

Technorati Tags: ,
posted on Monday, December 15, 2008 1:37 PM