get string between other string vb.net

前端 未结 4 1725
小鲜肉
小鲜肉 2021-01-19 15:32

I have code below. How do I get strings inside brackets? Thank you.

Dim tmpStr() As String
    Dim strSplit() As String
    Dim strReal As String
    Dim i A         


        
4条回答
  •  生来不讨喜
    2021-01-19 16:14

    Dim src As String = "hello (string1) there how (string2) are you?"
    Dim strs As New List(Of String)
    
    Dim start As Integer = 0
    Dim [end] As Integer = 0
    
    While start < src.Length
    
        start = src.IndexOf("("c, start)
        If start <> -1 Then
            [end] = src.IndexOf(")"c, start)
            If [end] <> -1 Then
                Dim subStr As String = src.Substring(start + 1, [end] - start - 1)
                If Not subStr.StartsWith("(") Then strs.Add(src.Substring(start + 1, [end] - start - 1))
            End If
        Else
            Exit While
        End If
    
        start += 1 ' Increment start to skip to next (
    
    End While
    

    This should do it.

    Dim result = Regex.Matches(src, "\(([^()]*)\)").Cast(Of Match)().Select(Function(x) x.Groups(1))
    

    Would also work.

提交回复
热议问题