VBA 中的正则没有其他语言灵活,不过可以分两步实现:
上面的条件不满足时再用另一个正则表达式匹配双引号之间的内容(不包括双引号)
参考代码:
' 引用 Microsoft VBScript Regular Expressions 55 (请根据自己的电脑环境选择)Sub MatchText()
Dim rExp As RegExp
' 待测试字符串
Const STR_TEST As String = "abc""abcd"""
'匹配开头且包含开头
Const STR_PATTERN_1 As String = "^([\s\S]+)""\1"""
'匹配双引号之间的内容(不包括双引号)
Const STR_PATTERN_2 As String = """([\s\S]+)"""
Set rExp = New RegExp
With rExp
MultiLine = True
Global = True
IgnoreCase = True
Pattern = STR_PATTERN_1
' / 待测字符串不是 abc"abc" 的形式 /
If Not Test(STR_TEST) Then
Dim mItem As Match
Dim mcAll As MatchCollection
Pattern = STR_PATTERN_2
Set mcAll = Execute(STR_TEST)
' / 遍历匹配集合中的子匹配 /
For Each mItem In mcAll
MsgBox mItemSubMatches(0)
Next
Set mItem = Nothing
Set mcAll = Nothing
End If
End With
Set rExp = Nothing
End Sub
运行结果:
圈着的那一句改成以下看看temp= Replace(Rng, "")
if len(temp)<len(Rng) then
n=n+1
Sheet3Cells(RngRow+n, 2) =temp
end if
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)