Stefan Đorđević Stefan Đorđević - 5 months ago 14
Vb.net Question

Get all after character in Regex

I need button that will seperate words from left and right of

:
character. I know how to get all before
:
, but how could I get all after
:
?

My code:

TextBox1.Text = System.IO.File.ReadAllText(OpenFileDialog1.FileName)
Dim findtext2 As String = "(.*?)(?=:)" 'Gets all before ":"
Dim myregex2 As String = TextBox1.Text
Dim doregex2 As MatchCollection = Regex.Matches(myregex2, findtext2)
Dim matches2 As String = ""
For Each match2 As Match In doregex2
matches2 = matches2 + match2.ToString + Environment.NewLine
Next
MsgBox(matches2)


I tried following with
Dim findtext2 As String = "(?=:)(.*?)"
, but it doesn't work.

Answer

If you know that your string would contain a colon, you could use a String.Split() call instead of a regular expression to get the sections you need :

If TextBox1.Text.Contains(":") Then
    ' Split using the colon '
    Dim sections = TextBox1.Text.Split(":")
    ' Get the first part '
    Dim before = sections(0)
    ' Get the second part '
    Dim after = sections(1)
End If