正则表达式没有获得街道名称

时间:2016-03-25 21:39:54

标签: regex string parsing text street-address

我正在解析这样的文字:

T-SHIRT SIZE 34CM BUSINESS LOCATED: MONTANA 356

我已经制作了这个正则表达式:

([A-Z]+) (\d\d\d\d\d|\d\d\d\d|\d\d\d|\d\d)

匹配:

SIZE 34

但我希望它匹配:

MONTANA 356

你能帮助我吗?

更明确一点:我想避免匹配“34号”,因为后面跟着一个字符......我希望正则表达式只有在想要成为“或”之后才能进行匹配匹配的字符串

3 个答案:

答案 0 :(得分:1)

以下是一项应该有效的修改:([A-Za-z]+) \b(\d{2,5})\b

您需要指定哪些符号对名称有效(我包括大写和小写字母)。我还使用简写来指定2到5位数。

关键部分是用词边界\b来围绕数字。这会解决您的问题吗?

答案 1 :(得分:1)

你可以尝试使用这个表达式吗? ([\w]+)\s(\d\d\d\d\d|\d\d\d\d|\d\d\d|\d\d)\b

答案 2 :(得分:1)

发生我尝试在VBA Excel中学习一些正则表达式。 如果您没有为RegEx提供代码,则无法回答。 在VBA中,模式确实匹配"大小34和#34;和"蒙大拿州356"。 MatchCollection数组中的第一个和第二个位置。难道你只返回第一场比赛吗?

' *** /更新/ 我用它作为测试函数。

Function RegExpTest(patrn As String, strTest As String) As Variant
Dim regex As New VBScript_RegExp_55.RegExp
Dim Match As Match, Matches As MatchCollection
Dim cnt As Integer, cmb() As Variant
If patrn <> "" Then
    With regex
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = patrn
    End With
    If regex.test(strTest) Then
        Set Matches = regex.Execute(strTest)
        cnt = Matches.Count
        ReDim cmb((cnt * 3) - 1)
        Dim i As Integer: i = 0
        For Each Match In Matches
            cmb(i) = " m:" & Match.Value & ","
            i = i + 1
            cmb(i) = "i:" & Match.FirstIndex & ","
            i = i + 1
            cmb(i) = "c:" & Match.Length & " |"
            i = i + 1
'            cmb(i) = "sub:" & Match.SubMatches.Count & "|"
'            i = i + 1
        Next
        RegExpTest = Join(cmb)
    Else
        RegExpTest = 0
    End If
End If
Set regex = Nothing
End Function
相关问题