「A1(Value)」などの文字列を部分文字列にして、 「Value」を返すにはどうすればよいですか?
3 に答える
1
を使用できますRegularExpression
:
Dim str = "A1(Value)...(anotherValue)"
Dim pattern = "\(([^)]*)\)"
Dim regex = New System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.Compiled)
Dim firstBracket = regex.Match(str)
If firstBracket.Value.Length <> 0 Then
Dim inFirstBracket = firstBracket.Value.Substring(1, firstBracket.Value.Length - 2)
'Value'
End If
于 2012-07-12T09:13:00.897 に答える
0
正規表現で確認してください
Dim result As String
Dim txt As String ="A1(Value)"
Dim re1 As String=".*?"
Dim re2 As String="((?:[a-z][a-z]+))"
Dim r As Regex = new Regex(re1+re2,RegexOptions.IgnoreCase Or RegexOptions.Singleline)
Dim m As Match = r.Match(txt)
If (m.Success) Then
Dim word1=m.Groups(1)
result = word1.ToString()
End If
入手: http://txt2re.com/index-vb.php3?s=A1%28Value%29&2
または、文字列を 3 番目の文字から長さ 1 に分割するだけです
于 2012-07-12T09:08:57.773 に答える
0
正規表現を使用したくない場合IndexOf
は、ブラケットの場所を見つけて、ブラケットSubString
内の文字列の部分に戻るために使用できます。
Dim txt As String = "A1(Value)"
Debug.WriteLine(txt.Substring(txt.IndexOf("(") + 1, txt.IndexOf(")") - txt.IndexOf("(") - 1))
文字列に開き括弧と閉じ括弧が含まれていない場合、例外がスローされるため、エラー チェックを追加することをお勧めします。
于 2012-07-12T13:12:18.063 に答える