我创建了一个VBA宏,它能够找到以下字符串的可能性。
With mainSel.Find
.ClearFormatting
.MatchWildcards = True
.Text = "[\{\[][a-zA-Z0-9\,\.\:\-;^l^13 ]@[\}\]]"
.Wrap = wdFindContinue
End With
但目前的情况是,当它在文档中找到一个字段代码时,它会跳过所有其他匹配项。如果我删除上面正则表达式中的空格,它将在字段代码之后处理字符串,但它正在跳过其中包含空格的字符串,如{xxxx}。
EX:这是一个例子{xxxx} -getting processed.this是[fieldcode]。这是未经处理的字符串{xxx}。
答案 0 :(得分:0)
你需要一个while循环而不是出现,因为如果find操作成功,Execute
会返回True
:
Option Explicit
Sub TestFind()
Dim Content As Range
Dim i As Long
Set Content = ActiveDocument.Content
' let's print what we got so far
Debug.Print "All text is:" & vbNewLine & Content.Text
With Content.Find
.ClearFormatting
' our find loop
Debug.Print "Occurences: "
While .Execute(MatchWildcards:=True, Wrap:=wdFindStop, FindText:="[\{\[][a-zA-Z0-9\,\.\:\-;^l^13 ]@[\}\]]")
' count occurences
i = i + 1
' lets bold text just for testing purposes
Content.Bold = True
' and also print each occurence
Debug.Print vbTab & "Occurence #" & i & vbTab & Content.Text
Wend
End With
End Sub
输出:
All text is:
{xxxxx}
asdasd
{ xxxx }
As{asd}
asff
{xxx }
{xxxx, xxxx}
safasf
{ xxxx,xxxx}
{xxxx,xxxx }
{ xxxx, xxxx }
Occurences:
Occurence #1 {xxxxx}
Occurence #2 { xxxx }
Occurence #3 {asd}
Occurence #4 {xxx }
Occurence #5 {xxxx, xxxx}
Occurence #6 { xxxx,xxxx}
Occurence #7 {xxxx,xxxx }
Occurence #8 { xxxx, xxxx }
此外,这根本不是一个正则表达式,并且thoose模式真的很粗糙。如果您需要真正的正则表达式,请查看here。