I’ll have to read a regex tutorial – so far I have a mental block in this area – right now I am just trying to match the following pattern:
ab234_12345_45678_afddsyyht788959
I am trying to extract the third (likely numeric) item surrounded by underscores (if the string has the wrong pattern, return invalid format, of course).
Something like:
Dim strOriginal As String = "ab234_12345_45678_afddsyyht788959"
Dim strFound As String
Try
Dim matches As MatchCollection
Dim regexStuff As New Regex("_.*?_")
matches = regexStuff.Matches(strOriginal)
strFound = matches.Item(0).Groups(2).Value.ToString
Catch
strFound = "invalid"
End Try
MessageBox.Show(strFound)
The problem is with the pattern – I can’t figure out anything that works… (I have tried a few other patterns too)
Use this regex
Match the
\1groupIf you want the third group to be numeric replace
([A-Za-z0-9]+)with(\d+)