我正在尝试在某些数据中捕获定制的html样式标记的内容。
然后我想用占位符替换内容和周围标记。
我正在使用objMatch.Value和objMatch.SubMatches(0)分别访问“内容+标签”和“仅内容”。但是,标签的斜角括号似乎导致这两个都返回内容。
此分解:
"<MyTag\b[^>]*>(.*?)</MyTag>"
objMatch.Value和objMatch.SubMatches(0)都只返回内容。
此作品
...但如果我将所有有角度的括号更改为美元符号,请执行以下操作:
"$MyTag\b[^$]*$(.*?)$/MyTag$"
objMatch.Value返回“contents + tag”,objMatch.SubMatches(0)返回“仅内容”。正如预期和期望。
有人可以解释为什么会这样,和/或我如何改变我的正则表达式来解决它?
完整代码
Dim oRegEx
Set oRegEx = New RegExp
oRegEx.IgnoreCase = True
oRegEx.Global = True
oRegEx.Pattern = "<MyTag\b[^>]*>(.*?)</MyTag>"
dim matches()
dim replacements()
dim i: i = 0
dim objMatch
For Each objMatch in oRegEx.Execute(stringToTest)
redim preserve matches(i)
redim preserve replacements(i)
replacements(i) = objMatch.Value
matches(i) = objMatch.SubMatches(0)
i = (i + 1)
Next