VBA 正则表达式中的后向查找?

发布于 2025-01-02 06:04:58 字数 137 浏览 2 评论 0原文

有没有办法在 VBA 正则表达式中进行负向和正向向后查找?

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

我正在使用 regExp 对象。

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

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!

I am using the regExp object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離殇 2025-01-09 06:04:58

VBA 提供前瞻(正向和负向),但不一致不提供后向查看

我见过的将 Regex 与 VBA 结合使用的最佳示例是 本文,作者:Patrick Matthews。

[使用执行而不是替换更新示例]

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

  • 它会跳过任何以 aA 开头的单词(不区分大小写)。
  • 对于所有不以 a/A 开头的单词,它返回从第二个字符开始的所有内容。这是通过使用子匹配来实现的(( and ) 内的模式是第一个子匹配)。

要了解完整的模式,您可以参考regex101的说明

    Sub TestString()
       MsgBox ReducedText("cfat dcat")
       ' results in:        fat  cat
       MsgBox ReducedText("Sat all over the hat again")
       ' results in:        at      ver  he  at
    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

VBA offers lookahead (both positive and negative) but rather inconsistently not lookbehind.

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

[Updated example using Execute rather than Replace]

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

  • It skips any words starting with a or A (case-insensitivity).
  • For all words not starting with a/A it returns everything from the second character on. This is achieved by using a submatch (the pattern inside ( and ) is the first submatch).

To understand the complete pattern, you may refer to the explanation on regex101.

    Sub TestString()
       MsgBox ReducedText("cfat dcat")
       ' results in:        fat  cat
       MsgBox ReducedText("Sat all over the hat again")
       ' results in:        at      ver  he  at
    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
谜泪 2025-01-09 06:04:58

将 ^A 放入非捕获组并使用 Match 对象的 SubMatches 属性来获取匹配值怎么样?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文