正则表达式模式选择正确的子字符串,但在运行宏 vba 时抛出错误

Posted

技术标签:

【中文标题】正则表达式模式选择正确的子字符串,但在运行宏 vba 时抛出错误【英文标题】:RegEx pattern selects the proper substring but throws error when running macro vba 【发布时间】:2021-08-12 02:01:22 【问题描述】:

我正在尝试使用正则表达式删除两个字符串中逗号, 前面的[(左括号)和?(问号)之后的所有内容。

我有这样的输入:

Together, Let's End Their War: Promoting a Culture of Health among Veterans on the Gulf - How strongly do you value your relationship with [Field-2]?

Together, Let's End Their War: Promoting a Culture of Health among Veterans on the Gulf - During the Clinical Scholars Program, with [Field-2], have you partnered new project(s) other than your team's Wicked Problem Impact Project?

所以我想删除第一个字符串中的? 和第二个字符串中的以下内容

, have you partnered new project(s) other than your team's Wicked Problem Impact Project?

我想结束

Together, Let's End Their War: Promoting a Culture of Health among Veterans on the Gulf - How strongly do you value your relationship with [Field-2]

Together, Let's End Their War: Promoting a Culture of Health among Veterans on the Gulf - During the Clinical Scholars Program, with [Field-2]

我有 (?<=]),\s*([^,])+|\?

模式似乎在捕捉我想要的东西

但是当我运行我的宏时,我得到了Method 'Replace' of object 'IRegEep2' failed

https://regex101.com/r/c9lDYD/1

我已经用我的宏运行了许多其他正则表达式模式,没有任何问题,所以不确定是什么问题。

Sub findReplace()
Dim outArray As Variant
Dim regEx As New RegExp
Dim ws As Worksheet
Dim i As Long

  Dim strPattern As String: strPattern = "(?<=]),\s*([^,])+|\?"
  Dim strReplace As String: strReplace = ""
        
  With regEx
    .Global = True
    .MultiLine = True
    .IgnoreCase = False
    .Pattern = strPattern
 End With
    
 Set ws = ThisWorkbook.Sheets("Helper_1Filted")

 With ws.Range("K1:K50")
    
      outArray = .value
      For i = 1 To UBound(outArray, 1)
          outArray(i, 1) = regEx.Replace(outArray(i, 1), strReplace)
      Next i
        
      .value = outArray
        
  End With
    
End Sub

【问题讨论】:

【参考方案1】:

我认为 vba 不支持后视,但如果问号应该出现在匹配逗号和方括号之间的部分之后,您可以使用捕获组而无需替换 |

当使用替换 | 时,问号将匹配字符串中的任何位置。

您可以使用捕获组和negated character class [^

在替换使用组1$1

(,[^\]\[,]*\[[^\]\[]*])[^?]*\?
( 捕获第 1 组 , 匹配逗号 [^\]\[,]* 匹配 0+ 次除逗号或 [] 之外的任何字符 \[[^\]\[]*] 匹配来自 [...] )关闭第一组 [^?]* 匹配除问号以外的任何字符 0+ 次 \?匹配问号

Regex demo

或带有可选捕获组的较短版本:

(\])(?:,\s*[^,]+)?\?

Regex demo

【讨论】:

谢谢,不知道VBA不支持lookbehind 【参考方案2】:

检查后,可能当前的VBA regex library 5.5不支持你代码中的回调函数,因为我做测试时有警告

?<=]

通过修改它,它可以通过替换您示例中的问号来工作,尽管其他句子我可能不是 100% 确定,我只更改了开头部分,仅供参考。

(^]),\s*([^,])+|\?

【讨论】:

以上是关于正则表达式模式选择正确的子字符串,但在运行宏 vba 时抛出错误的主要内容,如果未能解决你的问题,请参考以下文章

使用宏删除 VB.NET 代码中的所有注释和空行的 Visual Studio 正则表达式

正则表达式

初学正则表达式

正则表达式知识详解

我需要一个正则表达式结果,不包括匹配模式的开头和结尾的子字符串

正则表达式