从正则表达式捕获中排除换行符

时间:2016-04-19 14:32:40

标签: regex excel-vba vba excel

我意识到类似的问题有been asked before并已回答,但在我尝试了该答案中提出的解决方案后问题仍然存在。

我想编写一个Excel宏来将多行字符串分成多行,修剪包含换行符的空格。这是我的代码:

Sub testRegexMatch()
    Dim r As New VBScript_RegExp_55.regexp
    Dim str As String
    Dim mc As MatchCollection
    r.Pattern = "[\r\n\s]*([^\r\n]+?)[\s\r\n]*$"
    r.Global = True
    r.MultiLine = True
    str = "This is a haiku" & vbCrLf _
        & "You may read it if you wish   " & vbCrLf _
        & "   but you don't have to"
    Set mc = r.Execute(str)
    For Each Line In mc
      Debug.Print "^" & Line & "$"
    Next Line
End Sub

预期产出:

^This is a haiku$
^You may read it if you wish$
^but you don't have to$

实际输出:

^This is a haiku
$
^
You may read it if you wish   
$
^
   but you don't have to$

我在Regex101上尝试了同样的事情,但这似乎显示了正确的捕获,所以它必须是VBA的正则表达式引擎的怪癖。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您只需要通过SubMatches()访问捕获的值:

  

执行正则表达式时,在捕获括号 中包含 子表达式时,可能会产生零个或多个子匹配。 SubMatches集合中的每个项目都是由正则表达式找到并捕获的字符串。

这是我的演示:

Sub DemoFn()
   Dim re, targetString, colMatch, objMatch
   Set re = New regexp
   With re
     .pattern = "\s*([^\r\n]+?)\s*$"
     .Global = True              ' Same as /g at the online tester
     .MultiLine = True           ' Same as /m at regex101.com
   End With
   targetString = "This is a haiku  " & vbLf & "  You may read it if you wish " & vbLf & "    but you don't have to"
   Set colMatch = re.Execute(targetString)
   For Each objMatch In colMatch
     Debug.Print objMatch.SubMatches.Item(0) ' <== SEE HERE
   Next
End Sub

打印:

This is a haiku
You may read it if you wish
but you don't have to