0

I have a string like so: asdf text:a123a)! testing. I would like to get this string and extract this string: a123a)!.

I'm trying to do it like so:

If TextBox.Text.Trim.ToUpper.Contains("TEXT:") Then
        Dim SearchStringFilter As String = TextBox.Text.Trim
        Dim FilterString As Match = Regex.Match(SearchStringFilter, "text:([A-Za-z0-9\-])", RegexOptions.IgnoreCase)
        If FilterString .Success Then
            Dim SubFilterString As String = StateFilterString.Value.ToUpper.Replace("TEST:", "")
            MessageBox.Show(SubFilterString)
        End If
    End If

However, this code does not seem to be working correctly. It will only output "1". Can anyone help?

3
  • You seem to know how to use Contains, why not use Contains? Regex is for pattern matching and you are looking for an exact string. Commented May 24, 2018 at 13:31
  • @JacobH Hey Jacob! Well, I thought about that, but I don't know how to get "a123a)!" using .contains. Commented May 24, 2018 at 13:36
  • @MatSnow Hello! No, I just want to extract "a123a)!" anything with a space after that should be omitted. So "asdf text:a123a)! testing" would become "a123a)!". Commented May 24, 2018 at 13:36

3 Answers 3

3

You could just use IndexOf to get the part you need.

    Dim str As String = "asdf text:a123a)! testing"
    Dim index1 As Integer = str.IndexOf("text:") + "text:".Length
    Dim index2 As Integer = str.IndexOf(" ", index1)
    Dim result As String = str.Substring(index1, index2 - index1)

You'll need to add error checking.

If there's no space, you could get everything until the end.

    If index2 = -1 then
        result = str.Substring(index1)
    Else
        result = str.Substring(index1, index2 - index1)
    End If
Sign up to request clarification or add additional context in comments.

4 Comments

Hey, it looks like index2 stays as -1 no matter what, resulting in an error for the fourth line.
@JasonMills if that is the case, then there are no space after "text:" and you should get the string until the end. str.Substring(index1)
I marked your reply as an answer, but just wondering, how would I do the same thing but if there isn't a space?
Works like an absolute charm. Thank you so much@
0

You could use split :-

Dim Subject as string = "asdf text:a123a)! testing"
'
Dim DivArr As Char() = {":"c, " "c}
'split into segments using ":" and " " as separators - get 3rd element
Dim part3 As String = Subject.Split(DivArr)(2)

or one line

Dim part3 As String = Subject.Split({":"c, " "c})(2)

Comments

0

Regex.Split lets you split case-invariantly while keeping the original case of the text. Very handy.

Sub Ding()
    Dim firstItem As Boolean = True
    For Each s As String In Regex.Split(TextBox1.Text, "text:", RegexOptions.IgnoreCase) ' <-- this is the magic
        If firstItem Then
            firstItem = False 'the first item didn't have 'text:', so ignore it
        Else
            If s.Contains(" ") Then
                s = s.Split(" ").First
            End If
            MsgBox(s)
        End If
    Next
End Sub

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.