目前,我在VBA中的正则表达式捕获了花括号(包括花括号)中的所有内容:
Pattern = "\{\w*\}"
是否可以省略正则表达式级别的花括号?
因此{Computer1} {Computer2}
成为Computer1 Computer2
完整代码如下:
Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
.Global = True
.IgnoreCase = True
.MultiLine = True
.Pattern = "\{\w*\}"
End With
在支持回溯的正则表达式中,您可以执行以下操作:
(?<={)\w*(?=})
这仅匹配括号的内容。请注意,为避免返回空字符串,请将更改*
为+
但是VBscript不支持向后看。
相反,您可以使用括号将内容捕获到第1组:
{(\w+)}
这是RegexBuddy生成的代码(无法注释,因为我不使用VBScript):
Dim myRegexp, contentOfBraces, myMatches, myMatch As Match
Dim myRegexp As RegExp
Set myRegexp = New RegExp
myRegexp.Pattern = "{(\w+)}"
Set myMatches = myRegexp.Execute(SubjectString)
If myMatches.Count >= 1 Then
Set myMatch = myMatches(0)
If myMatch.SubMatches.Count >= 1 Then
contentOfBraces = myMatch.SubMatches(1-1)
Else
contentOfBraces = ""
End If
Else
contentOfBraces = ""
End If
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句