2015-07-28 51 views
0

我想用Regex.Ismatch功能在VB.net到***anything***Customer No. 9999999 ***anything***正則表達式可以匹配「*** ***任何客戶號9999999 *** ***東西」

即「客戶號」匹配模式 - >靜態 後「客戶號碼」7數字字符 - >動態

我的正則表達式迄今嘗試^ .*Customer No\. [0-9]+ .*$。我很接近,但如果只有Customer No. 9999999如果有前綴和後綴,只匹配它不接受

+1

逃亡'.'通過前面的'\\''。即使在評論中,我已經通過\\' – Tushar

+0

轉義'\\'不明白您能否請您重寫 – user3800108

+2

Works For Me。檢查[這裏](https://regex101.com/r/yC5iO5/1) – Tushar

回答

1

給這個模式的嘗試:

"Customer No\. \d{7}" 

這是捕獲字面「客戶編號」加上連續7個位數成whic ...你可以訪問,如:

Imports System.Text.RegularExpressions 

Module Module1 
    Sub Main() 
     Dim data As String = "***anything***Customer No. 9999999 ***anything***" 
     Dim matcher As Match = Regex.Match(data, "Customer No\. \d{7}") 
     If matcher.Success Then 
      Console.WriteLine(matcher.Value) 
     End If 
    End Sub 
End Module 

結果:

Customer No. 9999999 
+0

好吧,這就是你想要的,順便說一句。 「。*?'是多餘的,因此也是括號,它只是組0。 – maraca

+0

它匹配即使」客戶號「。我想要區分大小寫 – user3800108

+0

@ user3800108你說的是一個字符串「*** anything *** Customer no。9999999 ***什麼***「,正則表達式仍然匹配?我沒有看到結果 – Shar1er80

1

C#:

string s = " Customer No. 9999999"; 
       Regex regex = new Regex(@"^Customer No. +[0-9]{7}$"); 
       Match match = regex.Match(s); 
       if (match.Success) 
       { 
        Console.WriteLine(match.Value); 
       } 
       else 
       { 
        Console.WriteLine("not match"); 
       } 

VB:

Dim s As String = "Customer No. 9999999" 
Dim regex As New Regex(@"^Customer No. +[0-9]{7}$") 
Dim match As Match = regex.Match(s) 
If match.Success Then 
    Console.WriteLine(match.Value) 
Else 
    Console.WriteLine("not match") 
End If 
+0

嗨如果我需要匹配'dsxzx客戶編號9999999 xccx' – user3800108

+0

@ user3800108你的意思是dsxzx是一個5個字母的單詞嗎? –

+0

不,我很接近*客戶號碼。 [0-9] + * $' 但不接受'客戶編號9999999' 我想匹配這 '***什麼**客戶編號9999999 *** ***東西' – user3800108