2012-07-12 23 views

回答

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

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 
0

如果你不想使用正則表達式,那麼你可以使用IndexOf找到支架的位置和SubString在括號內返回字符串的一部分:

Dim txt As String = "A1(Value)" 
Debug.WriteLine(txt.Substring(txt.IndexOf("(") + 1, txt.IndexOf(")") - txt.IndexOf("(") - 1)) 

注意,如果字符串不包含開放和緊密的括號這將拋出一個異常,所以你可能想添加一些錯誤檢查

相關問題