我想在VB.net中使用Regex.Ismatch
函数来匹配模式***anything***Customer No. 9999999 ***anything***
即"客户编号" - >静态的 在"客户编号" 7个数字字符 - >动态
到目前为止,我正在试用^ .*Customer No\. [0-9]+ .*$
。我很接近,但如果只有Customer No. 9999999
只有前缀和后缀
答案 0 :(得分: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
答案 1 :(得分:1)
尝试使用此模式:
"Customer No\. \d{7}"
这是捕获文字"客户编号"加上你可以访问的7个连续数字,如:
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