I'm interested in parsing and extracting the IDs found in a document. The IDs are found throughout the document in the this format:
The "1FG3392MQ9" is an example of such an ID (it will always appear between the IDStart and IDEnd tokens as in the above example). There will be text before and possibly after each occurrence and they could appear once on a given line or many times on a line.
Here's a utility function I have to parse and extract them:
Public Shared Function ParseALLDocumentIDs(text As String) As List(Of String) Dim allParsedIDs As New List(Of String) Dim IDregex As String = "IDStart=(.+?)&IDEnd" Dim matches As MatchCollection = Regex.Matches(text, IDregex, RegexOptions.IgnoreCase Or RegexOptions.Multiline) For Each m As Match In matches If m.Success Then Dim parsedID = m.GetFristCapture() If Not String.IsNullOrWhiteSpace(parsedID) Then allParsedIDs.Add(parsedID) End If End If Next Return allParsedIDs End Function
Upon each match, the following extension method is used (defined in another module) to get the first group capture of a match:
<Extension()> Public Function GetFristCapture(m As Match) As String ' Gets the first group capture "(...)" in a regex match. ' Returns empty string if not a match or there is no group capture found Dim capturedValue As String = "" If m IsNot Nothing AndAlso m.Success AndAlso m.Groups.Count > 1 Then ' Note: The first group is the entire match itself, ' so Group(1) is what we want, not Group(0) If m.Groups(1).Success Then capturedValue = m.Groups(1).Value.Trim End If End If Return capturedValue End Function
How does the Regex look? Am I missing anything? The idea was to capture all text between any occurrences of those two tokens, and do it non-greedily so that it doesn't capture across multiple tokens.
How does overall approach look with getting these IDs? Is there a cleaner way?