Hi,
You will struggle to do this without looking over your sentence more than once. Regex cannot step backwards to look at substrings that have already been matched.
If you would like to stick with Regex then you could do something like this:
Although you should be able to write a better function to achieve the same goal using different methods e.g. String.Split, For-Next and Concatenation.VB.NET Code:
Dim str As String = "This is a test" Dim str2 As String = str.Remove(0, str.IndexOf(" ") + 1) Dim Results As MatchCollection = Regex.Matches(str, "\b\w+\b\s?\b\w+\b") Dim Results2 As MatchCollection = Regex.Matches(str2, "\b\w+\b\s?\b\w+\b") Dim output As String = Nothing For i As Integer = 0 To Results.Count - 1 output += Results.Item(i).Value & vbCrLf If i <= Results2.Count - 1 Then output += Results2.Item(i).Value & vbCrLf End If Next MsgBox(output)
Hope this helps![]()




Reply With Quote