2016-05-18 4 views
1

実際には、これを実行して丸めブラケット内の値を検出し、丸めブラケット内の値に丸めます。例えばこの場合ブラケット内に複数の開閉ブラケットがある場合に、開いているブラケットに属する閉鎖ブラケットを検出する

Dim h As String = "ROUNDING(30.98998(10))*2+3"  
Dim r As String = h.ToString.Substring(h.ToString.IndexOf("ROUNDING(") + 1, h.ToString.IndexOf(")") - 1 - h.ToString.IndexOf("ROUNDING(")) 

ROUNDING(後もう一つ()あります。 ROUNDINGにあるかっこを閉じ括弧の終わりに合わせるにはどうすればいいですか?

ありがとうございます!

答えて

1

Functionを使用できます。

Public Function ReadInBetweenSameDepth(str As String, delimiterStart As Char, delimiterEnd As Char) As String 
    If delimiterStart = delimiterEnd OrElse String.IsNullOrWhiteSpace(str) OrElse str.Length <= 2 Then 
     Return Nothing 
    End If 
    Dim delimiterStartFound As Integer = 0 
    Dim delimiterEndFound As Integer = 0 
    Dim posStart As Integer = -1 
    For i As Integer = 0 To str.Length - 1 
     If str(i) = delimiterStart Then 
      If i >= str.Length - 2 Then 
       'delimiter start is found in any of the last two characters 
       Return Nothing 
      End If 
      'it means, there isn't anything in between the two 
      If delimiterStartFound = 0 Then 
       'first time 
       posStart = i + 1 
      End If 
      'assign the starting position only the first time... 
       'increase the number of delimiter start count to get the same depth 
      delimiterStartFound += 1 
     End If 
     If str(i) = delimiterEnd Then 
      delimiterEndFound += 1 
      If delimiterStartFound = delimiterEndFound AndAlso i - posStart > 0 Then 
       Return str.Substring(posStart, i - posStart) 
       'only successful if both delimiters are found in the same depth 
      End If 
     End If 
    Next 
    Return Nothing 
End Function 

(例えば()など)区切り文字が同じ「深さ」である場合、それは基本的にチェックします。

+1

私はちょうどRegExを使用してPHPでstackoverflowの人の助けを借りてそれをしたので、私はそれをやろうとしましたが、あなたのアイデアを放棄し、良い機能! –

+0

@ MasterDJonああ、ありがとう...私は光栄です。 – Ian

+0

ヘルプheheありがとう –

関連する問題