正则表达式 VbScript 捕获前瞻

3

I have a text with this format:

[AAAAA]xyzxyzxyz
[AAAAA]abcdefghi
[AAAAA]whatever
[BBBBB]aaaaaaaa
[BBBBB]cccccccc
[BBBBB]dddddddd
[CCCCC]ffffffff
[CCCCC]eeeeeeee

我希望能够在多项选择中捕获标签后的字符串,但不包含标签。
(?m)(?<=^\[BBBBBB\]).*$ 

获得一个数组:
[1]aaaaaaaa
[2]cccccccc
[3]dddddddd

例如,在我的编辑器上可以正常工作,但是在Excel VBA(vbscript.regex)中不支持预查。我能做的最好的就是:
 ((^\[BBBBBB\])(.*?)$) 

在第二步中将标签替换为空,这是与IT技术相关的内容。

是否存在一种好的方法将 : (?m)(?<=^[BBBBBB]).*$ 转换为VBA正则表达式引擎?

谢谢你的帮助。

大卫。

1个回答

2
这里的正向回顾不必要,因为它只是为了定义所需匹配的左侧上下文,并且没有重叠的匹配。因此,你可以安全地使用一个“消耗性”的模式,然后用捕获括号包围模式的其余部分或你感兴趣的部分,以便稍后获取第一组的内容。
.Pattern = "^\[B{5}\]\s*([^\r\n]*)"

.最好替换为[^\r\n]以排除\r的匹配,而{5}更方便地表示5个B

完整演示:

Sub DemoFn2()
   Dim re As RegExp
   Dim s As String
   Dim colMatch As MatchCollection, objMatch As Match

s = "[AAAAA]xyzxyzxyz" & vbCrLf & "[AAAAA] abcdefghi" & vbCrLf & "[AAAAA] whatever" & vbCrLf & "[BBBBB] aaaaaaaa" & vbCrLf & "[BBBBB] cccccccc" & vbCrLf & "[BBBBB] dddddddd" & vbCrLf & "[CCCCC] ffffffff" & vbCrLf & "[CCCCC] eeeeeeee"
Set re = New RegExp
With re
  .Pattern = "^\[B{5}\]\s*([^\r\n]*)"
  .Global = True              ' Same as /g at the online tester
  .MultiLine = True          ' Same as /m at regex101.com
End With

Set colMatch = re.Execute(s)
For Each objMatch In colMatch
  Debug.Print objMatch.SubMatches.Item(0)
Next
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接