ric*_*ier 14 regex vba negative-lookbehind lookbehind
有没有办法在VBA正则表达式中做出消极和积极的观察?
如果字符串以"A"开头,我想不匹配,所以我当前在模式的开头做^ A,然后删除match(0)的第一个字符.显然不是最好的方法!
我正在使用regExp对象.
bre*_*tdj 11
VBA提供了积极和消极的前瞻,但却不一致而不是后视.
我见过的使用Regex和VBA的最好例子是Patrick Matthews 撰写的这篇文章
[更新示例使用Execute而不是Replace]
虽然我不完全清楚你的用法,你可以使用这样的功能
对于所有不以a开头的单词,返回第二个字符的所有内容(使用子匹配 - 里面的模式(和子)匹配1 -
Sub TestString()
MsgBox ReducedText("cfat dcat")
MsgBox ReducedText("Sat all over the hat again")
End Sub
Function ReducedText(strIn As String) As String
Dim objRegex As Object
Dim objRegMC As Object
Dim objRegM As Object
Dim strOut As String
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.IgnoreCase = True
'not needed if matching the whole string
.Global = True
.Pattern = "\b[^a\s]([a-z]+)"
If .test(strIn) Then
Set objRegMC = .Execute(strIn)
For Each objRegM In objRegMC
strOut = strOut & objRegM.submatches(0) & vbNewLine
Next
ReducedText = strOut
Else
ReducedText = "Starts with A"
End If
End With
End Function
Run Code Online (Sandbox Code Playgroud)