正则表达式匹配" ***任何***客户编号9999999 ***任何***"

时间:2015-07-28 12:01:16

标签: regex vb.net

我想在VB.net中使用Regex.Ismatch函数来匹配模式***anything***Customer No. 9999999 ***anything***

即"客户编号" - >静态的 在"客户编号" 7个数字字符 - >动态

到目前为止,我正在试用^ .*Customer No\. [0-9]+ .*$。我很接近,但如果只有Customer No. 9999999只有前缀和后缀

,它才会接受

2 个答案:

答案 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