回顾 VBA 的正则表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9150552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:09:05  来源:igfitidea点击:

Lookbehind on regex for VBA?

regexvbalookbehindnegative-lookbehind

提问by rich tier

Is there a way to do negative and positive lookbehind in VBA regex?

有没有办法在 VBA 正则表达式中进行负面和正面回顾?

I want to not match if the string starts with "A", so I am currently doing ^A at the start of the pattern, then removing the first character of match(0). Obviously not the best method!

如果字符串以“A”开头,我想不匹配,所以我目前在模式的开头执行 ^A,然后删除 match(0) 的第一个字符。显然不是最好的方法!

I am using the regExp object.

我正在使用 regExp 对象。

采纳答案by brettdj

VBA offers positive and negative lookaheads but rather inconsistently not lookbehind.

VBA 提供正面和负面的前瞻,但不一致地不是后视。

The best example of using Regex with VBA that I have seen is this articleby Patrick Matthews

我见过的将 Regex 与 VBA 结合使用的最佳示例是Patrick Matthews 的这篇文章

[Updated example using Executerather than Replace]

[更新示例使用Execute而不是Replace]

While I am not completely clear on your usage you could use a function like this with

虽然我不完全清楚你的用法,但你可以使用这样的函数

  • skips any words starting with A
  • for all words not starting with a it returns everything from the second character on (using a submatch - the pattern inside (and )is submatch 1 -

    Sub TestString()
    MsgBox ReducedText("cfat dcat")
    MsgBox ReducedText("Sat all over the hat again")
    End Sub
    
    
    Function ReducedText(strIn As String) As String
    Dim objRegex As Object
    Dim objRegMC As Object
    Dim objRegM As Object
    Dim strOut As String
    Set objRegex = CreateObject("vbscript.regexp")
    With objRegex
      .IgnoreCase = True
      'not needed if matching the whole string
      .Global = True
      .Pattern = "\b[^a\s]([a-z]+)"
      If .test(strIn) Then
          Set objRegMC = .Execute(strIn)
          For Each objRegM In objRegMC
            strOut = strOut & objRegM.submatches(0) & vbNewLine
          Next
          ReducedText = strOut
      Else
        ReducedText = "Starts with A"
      End If
    End With
    End Function
    
  • 跳过任何以 A 开头的单词
  • 对于所有不以 a 开头的单词,它返回从第二个字符开始的所有内容(使用子匹配 - 里面的模式(并且)是子匹配 1 -

    Sub TestString()
    MsgBox ReducedText("cfat dcat")
    MsgBox ReducedText("Sat all over the hat again")
    End Sub
    
    
    Function ReducedText(strIn As String) As String
    Dim objRegex As Object
    Dim objRegMC As Object
    Dim objRegM As Object
    Dim strOut As String
    Set objRegex = CreateObject("vbscript.regexp")
    With objRegex
      .IgnoreCase = True
      'not needed if matching the whole string
      .Global = True
      .Pattern = "\b[^a\s]([a-z]+)"
      If .test(strIn) Then
          Set objRegMC = .Execute(strIn)
          For Each objRegM In objRegMC
            strOut = strOut & objRegM.submatches(0) & vbNewLine
          Next
          ReducedText = strOut
      Else
        ReducedText = "Starts with A"
      End If
    End With
    End Function
    

回答by Sam Greenhalgh

How about putting the ^A in a non-captured group and using the SubMatches property of the Match object to get your matched value?

将 ^A 放在非捕获组中并使用 Match 对象的 SubMatches 属性来获取匹配值如何?