Reputation: 41
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?
Upvotes: 0
Views: 64
Reputation: 764
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
Upvotes: 0
Reputation: 1
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)
Upvotes: 0
Reputation: 12748
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
Upvotes: 3