How can I get characters between two other characters?

I have a string that at some point contains a character set in the following format [123] .

What I would like to do is get the characters between [] , but the characters between them never have the same length.

How do I do this in VB.NET?

+4
source share
5 answers
 Dim s As String = "foo [123]=ro bar" Dim i As Integer = s.IndexOf("[") Dim f As String = s.Substring(i + 1, s.IndexOf("]", i + 1) - i - 1) 
+12
source

You can do something like this.

 Dim val As String = str.Substring(1, str.Length - 2) // replace str with your string variable 
0
source
 Dim s As String = "nav[1]=root" dim result as String = s.Substring(s.IndexOf("[") + 1, s.IndexOf("]", s.IndexOf("[")) - s.IndexOf("[") - 1) 
0
source

You can use regex.

  Dim s, result As String Dim r As RegularExpressions.Regex s = "aaa[bbbb]ccc" r = New RegularExpressions.Regex("\[([^\]]*)\]") If r.IsMatch(s) Then result = r.Match(s).Value Else result = "" End If 
0
source

operator RegularExpressions.Regex("\[([^\]]*)\]") will return the value inside the bracket and the bracket of it!

I used this statement to return an IPAddress surrounded by () inside a long string: -

 Dim IPRegEx As Regex = New Regex("(?<=\().*?(?=\))") 
0
source

Source: https://habr.com/ru/post/1339167/


All Articles