我想提取括号括起来的值.
text ="{say}和{var2}以及{var3}的人
openingParen = InStr(text, "{")
closingParen = InStr(text, "}")
enclosedValue = Mid(text, openingParen + 1, closingParen - openingParen - 1)
phrasevariable1.Value = enclosedValue
phrasevariable2.Value = enclosedValue
phrasevariable3.Value = enclosedValue
Run Code Online (Sandbox Code Playgroud)
这段代码只适用于提取第一个值,有没有办法提取每个变量,并将它们放入文本框1 - > n中
您可以将正则表达式用于未知数量的候选令牌;
Dim matches As Object
Dim Re As Object: Set Re = CreateObject("vbscript.regexp")
Dim count As Long
Re.Pattern = "{(.*?)}"
Re.Global = True
Set matches = Re.Execute("The man who {said} and also {var2} as well as {var3}")
count = matches.count
For i = 0 To count - 1
MsgBox matches(i).submatches(0)
Next
Run Code Online (Sandbox Code Playgroud)
(为Microsoft VBScript正则表达式添加引用以进行早期绑定)