使用正则表达式提取ID

时间:2014-01-14 17:21:52

标签: regex vb.net

我正在编写一个以下列格式获取输入行的程序:

名字,姓氏,身份证号码,联系信息

我想使用正则表达式来获取格式为A ########的ID号,其中#可以是任何数字。

我用Google搜索并且无法理解VB的正则表达式模式,有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

一般来说,没有“VB的正则表达式”这样的东西。 VB.NET和C#都使用相同的.NET regular expression语法:

  

在.NET Framework中,正则表达式模式由特殊语法或语言定义,它与Perl 5正则表达式兼容,并添加了一些其他功能,如从右到左匹配。

然而,你的正则表达式应该是:

A\d{8}

表示:匹配A,然后匹配任意数字(\d)八次。

实用的VB.NET用法:

Dim input As String = "Firstname, Lastname, A12345678, contact info"
Dim id As String = Regex.Match(input, "A\d{8}").Value

答案 1 :(得分:0)

您可以简单地使用:

\bA\d+\b

在上下文中:

Imports System.Text.RegularExpressions

Module Module1
    Sub Main()
    Dim regex As Regex = New Regex("\bA\d+\b")
    Dim match As Match = regex.Match("Firstname, Lastname, A123456, Other stuff...")
    If match.Success Then
        Console.WriteLine(match.Value)
    End If
    End Sub
End Module

工作示例:http://regex101.com/r/pB0pR5