|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 weiyingde 于 2020-5-6 16:59 编辑
正则表达式:.Pattern = "[A-Z]+[\..]((?!([A-Z][\..]|【解析】[^\rA-Z\..]+\r)).)+"
问题:误配了【解析】文本段内容
要求:修改上面的正则表达式,使之只准确匹配选择题的选枝。
代码如下:
Sub 误配了解析()
Dim mt, mk, oRng As Range, n&, m&, str$, TT$
Dim rg As Range, arr(), x%, d As Object, k&, a
Set d = CreateObject("Scripting.Dictionary")
str = Replace(ActiveDocument.Content, Chr(7), "")
With CreateObject("vbscript.regexp")
.Global = True: .IgnoreCase = False: .MultiLine = True
.Pattern = "^\d+[^\r]*[\((]([A-Z])[\))]\r(?:(?!^\d+[^\r]+\r).)+"
For Each mt In .Execute(str)
fst = mt.firstindex
lgh = mt.Length
Set oRng = ActiveDocument.Range(fst, fst + lgh)
sr1 = mt.submatches(0) '获取答案
'下面的要求是:
'只匹配选枝,不匹配【解析】一段的任何文本。
.Pattern = "[A-Z]+[\..]((?!([A-Z][\..]|【解析】[^\rA-Z\..]+\r)).)+"
TT = Replace(oRng.Text, Chr(7), "")
For Each mk In .Execute(TT)
MsgBox mk
Next
Next
End With
End Sub |
|