Regex 如何使用正则表达式一次捕获字符串的多个部分?
我需要在一个较长的字符串Regex 如何使用正则表达式一次捕获字符串的多个部分?,regex,vba,Regex,Vba,我需要在一个较长的字符串strText中捕获几个字符串并对它们进行处理。我使用VBA strText: Salta pax {wenn([gender]|1|orum|2|argentum)} {[firstname]} {[lastname]}, ginhox seperatum de gloria desde quativo, dolus {[start]} tofi {[end]}, ([{n_night]} {wenn([n_night]|1|dignus|*|digni)}
strText
中捕获几个字符串并对它们进行处理。我使用VBA
strText
:
Salta pax {wenn([gender]|1|orum|2|argentum)} {[firstname]} {[lastname]},
ginhox seperatum de gloria desde quativo,
dolus {[start]} tofi {[end]}, ([{n_night]}
{wenn([n_night]|1|dignus|*|digni)}), cum {[n_person]}
{wenn([n_person]|1|felix|*|semporum)}.
Quod similis beruntur: {[number]}
我试图捕获strText的不同部分,都在花括号内:
{[firstname]}
-->firstname
wenn()
开始),我希望捕获方括号内的字符串加上以下数字-值对:{[gender]| 1 | orum | 2 | argentum}
-->gender
/1=orum
/2=argentum
我设法定义了一种模式,以获得上述任何一项任务
e、 g.\{\[(.+?)\]\}
捕获方括号内的字符串,但是我想一定有一种方法可以实现上述所有功能的模式?我不确定下面的代码是否对您有帮助。它使用
|
符号捕获这两种情况
Function extractStrings(strText As String) As MatchCollection
Dim regEx As New RegExp
Dim SubStrings As MatchCollection
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "(\{\[)(.+?)(\]\})|(wenn\(\[)(.+?)(\])(\|)(.+?)(\|)(.+?)(\|)(.+?)(\|)(.+?)(\)\})"
End With
On Error Resume Next
Set extractStrings = regEx.Execute(strText)
If Err = 0 Then Exit Function
Set extractStrings = Nothing
End Function
Sub test()
Dim strText As String
strText = "Salta pax {wenn([gender]|1|orum|2|argentum)} {[firstname]} {[lastname]},ginhox seperatum de gloria desde quativo,dolus {[start]} tofi {[end]}, ([{n_night]} " & _
"{wenn([n_night]|1|dignus|*|digni)}), cum {[n_person]}{wenn([n_person]|1|felix|*|semporum)}.Quod similis beruntur: {[number]}"
Dim SubStrings As MatchCollection
Dim SubString As Match
Set SubStrings = extractStrings(strText)
For Each SubString In SubStrings
On Error Resume Next
If SubString.SubMatches(1) <> "" Then
Debug.Print SubString.SubMatches(1)
Else
Debug.Print "wenn(" & SubString.SubMatches(4) & "|" & SubString.SubMatches(7) & "=" & SubString.SubMatches(9) & "|" & SubString.SubMatches(11) & "=" & SubString.SubMatches(13) & ")"
End If
Next SubString
End Sub
函数提取字符串(strText作为字符串)作为匹配集合
Dim regEx作为新的RegExp
作为匹配集合的Dim子字符串
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=“(\{\[)(.+?)(\]\})(wenn\(\[)(.+?)(\])(\\\)(.+?)(\\\)(.+?)(\\\)(.+?)(.+?)(\\\\)(.+?)(.+)(\\)”
以
出错时继续下一步
Set extractStrings=regEx.Execute(strText)
如果Err=0,则退出函数
设置字符串=无
端函数
子测试()
将strText设置为字符串
strText=“Salta pax{wenn([性别]| 1 | orum | 2 | argentum)}{[firstname]}{[lastname]}、ginhox Separatum de gloria desde quativo、dolus{[start]}豆腐{[end]}、([{n(u night]})和_
“{wenn([n_-night]{1{dignus{124;*.}),cum{[n_-person]}{wenn([n_-person]}1{felix{semporum}。Quod similis beruntur:{[数字]}”
作为匹配集合的Dim子字符串
作为匹配的暗淡子字符串
Set substring=extractString(strText)
对于子字符串中的每个子字符串
出错时继续下一步
如果SubString.SubMatches(1)”,则
调试.打印子字符串.子匹配(1)
其他的
调试.打印“wenn(&SubString.SubMatches(4)&“|”&SubString.SubMatches(7)&“=”&SubString.SubMatches(9)&“|”&SubString.SubMatches(11)&“=”&SubString.SubMatches(13)&”)
如果结束
下一个子串
端接头
对于每个循环,可以使用
遍历所有子字符串。我很清楚,正则表达式模式不是最优的,但至少它起到了作用。我能够将模式简化为\{\[(.+?)\]\}wenn\(\[(.+?)\]\\\\\\\\\(.+?)\\\\\.+?)\\\(.+?)\\\\\\\\\\.+?)\\\\\\\\\\
以获得所需的准确结果:谢谢!如果是的话,请接受我的回答。非常感谢。