Reputation: 14615
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)
Upvotes: 0
Views: 620
Reputation: 5452
Use this regex
[A-Za-z0-9]+_[A-Za-z0-9]+_([A-Za-z0-9]+)
Match the \1
group
If you want the third group to be numeric replace ([A-Za-z0-9]+)
with (\d+)
Upvotes: 1