如何排除字符串开头和结尾中的任何单个字母,但是如果没有,则仍然匹配

时间:2018-12-21 11:20:05

标签: regex excel vba excel-vba

我正在尝试将地址从1个单元分隔为其他单元。我从一个包含所有比利时城市的大城市中匹配城市。目前,它工作正常,除了以下例外:

在比利时,我们有一个名为“开”的城市。您也可以在街道名称中找到这个词。由于我不知道用户是否会进入城市,然后进入街道,还是进入城市之后的街道,因此我必须将另一个区分开(我也不确定是否会使用大写字母)。

区别是,在街道的“开”处,无论是在开头还是结尾,几乎都会在其上粘贴字母字母。 示例:

'Onderstraat', 'Donderstraat', 'Avenue du Bon'

我已经尝试过了:

objRegExp.Pattern = "[^a-zA-ZàáâäÀÁÂÄèéêëÈÉÊËôöÔÖùúûüÙÚÛÜïÏçÇ]" & LCase(vaCitiesA(i)) & "[^a-zA-ZàáâäÀÁÂÄèéêëÈÉÊËôöÔÖùúûüÙÚÛÜïÏçÇ]"

但是当他们把城市放在最后时,那不匹配,因为在城市之后没有其他内容(在下面的示例中为Schaarbeek) 例如:“ blv Albert II 121aboîte1,1030 Schaarbeek”

我想要的是上面的内容,它将找到Schaarbeek,但是对于Onderstraat,Donderstraat或av du Bon,它不应该与城市“ On”匹配

1 个答案:

答案 0 :(得分:-1)

OP的工作代码已从问题中删除,并以答案形式发布:

Dim vaCitiesA As Variant
Dim xSelection As Range
Dim xCity As String
Dim rest As String

vaCitiesA = Array("Hody", "Hour", "Houx", "HOVE", "Hyon", "Impe", "Izel", "Jeuk", "Kain", "Laar", "Lauw", "LEDE", "Leke", "LENS", "Leut", "LIER", "LINT", "Mark", "Mazy", _
    "Mean", "Meer", "Mere", "Meux", "Moen", "Moha", "MONS", "Mont", "Mont", "Muno", "NATO", "NIEL", "Nimy", "OHEY", "Oizy", "OLEN", "OLNE", "Omal", "Onoz", _
    "Orcq", "Oret", "Paal", "PECQ", "PEER", "Perk", "Redu", "Reet", "Roly", "Roux", "RTBF", "Seny", "Soye", "Suxy", "Thon", "Thys", "Velm", "VISE", "Vivy", _
    "Waha", "Ways", "Werm", "ZELE", "ANS", "ATH", "Aye", "Bra", "Eke", "Ere", "Goe", "HAM", "HUY", "Lot", "Mal", "MOL", "Ogy", "Pry", "Roy", _
    "Scy", "SOC", "Soy", "SPA", "Spy", "VRT", "VTM", "AS", "Lo", "My", "On")

Set xSelection = Application.Selection
For Each Rng In xSelection

    Dim allMatches As Object
    Dim objRegExp As Object
    'Initializing an Instance
    Set objRegExp = CreateObject("VBScript.RegExp")
    'Setting the Properties
    With objRegExp
        .Global = True
        .IgnoreCase = True
    End With

    xCity = ""
    rest = Rng.Value


    'a
    rest = Replace(rest, "à", "a")
    rest = Replace(rest, "â", "a")
    rest = Replace(rest, "á", "a")
    rest = Replace(rest, "À", "A")
    rest = Replace(rest, "Â", "A")
    rest = Replace(rest, "Á", "A")
    'e
    rest = Replace(rest, "ë", "e")
    rest = Replace(rest, "é", "e")
    rest = Replace(rest, "è", "e")
    rest = Replace(rest, "ê", "e")
    rest = Replace(rest, "Ë", "E")
    rest = Replace(rest, "É", "E")
    rest = Replace(rest, "È", "E")
    rest = Replace(rest, "Ê", "E")
    'o
    rest = Replace(rest, "ö", "o")
    rest = Replace(rest, "ô", "o")
    rest = Replace(rest, "Ö", "O")
    rest = Replace(rest, "Ô", "O")
    'u
    rest = Replace(rest, "ü", "u")
    rest = Replace(rest, "û", "u")
    rest = Replace(rest, "ù", "u")
    rest = Replace(rest, "ú", "u")
    rest = Replace(rest, "Ü", "U")
    rest = Replace(rest, "Û", "U")
    rest = Replace(rest, "Ù", "U")
    rest = Replace(rest, "Ú", "U")
    'i
    rest = Replace(rest, "ï", "i")
    rest = Replace(rest, "Ï", "I")
    'c
    rest = Replace(rest, "ç", "c")
    rest = Replace(rest, "Ç", "C")
    'special
    rest = Replace(rest, "'", "")
    rest = Replace(rest, "-", " ")

    Text = LCase(rest)

    For i = LBound(vaCitiesA) To UBound(vaCitiesA)
        '''''''''''''''''''''''''''''''''''''
    objRegExp.Pattern = "(^|[^a-zA-ZàáâäÀÁÂÄèéêëÈÉÊËôöÔÖùúûüÙÚÛÜïÏçÇ])" & LCase(vaCitiesG(i)) & "(?![a-zA-ZàáâäÀÁÂÄèéêëÈÉÊËôöÔÖùúûüÙÚÛÜïÏçÇ])"
    Set allMatches = objRegExp.Execute(Text)
    If objRegExp.Test(Text) Then
        xCity = vaCitiesA(i)
        Exit For
    End If
    ''''''''''''''''''''''''''''''''''''''''
Next i
If xCity <> "" Then
    'for the moment, I choose last match, but could be problematic if there is a short city like 'ON' in front of the street
        x = allMatches.Count - 1
        lCityPos = allMatches(x).FirstIndex
        rest = Replace(rest, Mid(rest, lCityPos + 1, Len(xCity) + 1), "", , 1)
    End If