使用正则表达式确定哪个捕获组在评估的字符串中匹配
Identify which capturing group was matched in the evaluated string using regex
你好,我是正则表达式的新手,我很难弄清楚如何在 VBA.
中使用正则表达式来获取在评估字符串中匹配的组
它可以出现在后跟 1 个或多个数字的字符串中的单词有 4 种或更多种不同的可能性:
- W点数=
- WR/KE-Point=
- WNr-点=
- SST_P-Nr =
其中一个单词在字符串中只出现一次
评估的字符串:
“3: CALL U(Base,EZSP,Nr1,Pr-nr=20,Offset=1,Path=2,WNr-Point=20,Pr=65,ON)” =11=]
使用的正则表达式:
(?:(W-点=)(\d*)|(SST_P-Nr=)(\d*)|(WR/KE-Point=)(\d*)|(WNr-点=)( \d*))
到目前为止一切正常:Example
问题: 识别匹配的 word/digit 对并获取其组号。现在我循环遍历结果并丢弃空的子匹配。有更好或更有效的方法吗?
提前致谢。
尝试
Sub test()
Dim s As String
s = "3: CALL U(Base,EZSP,Nr1,Pr-nr=20,Offset=1,Path=2,WNr-Point=20,Pr=65,ON)"
Dim Regex As Object, m As Object
Set Regex = CreateObject("vbscript.regexp")
With Regex
.Global = True
.MultiLine = False
.IgnoreCase = True
.pattern = "(W-Point|WR/KE-Point|WNr-Point|SST_P-Nr)( *= *)(\d*)"
End With
If Regex.test(s) Then
Set m = Regex.Execute(s)(0).submatches
Debug.Print m(0), "'" & m(1) & "'", m(2)
End If
End Sub
更新:捕获 = 和任何空格
你好,我是正则表达式的新手,我很难弄清楚如何在 VBA.
中使用正则表达式来获取在评估字符串中匹配的组它可以出现在后跟 1 个或多个数字的字符串中的单词有 4 种或更多种不同的可能性:
- W点数=
- WR/KE-Point=
- WNr-点=
- SST_P-Nr =
其中一个单词在字符串中只出现一次
评估的字符串: “3: CALL U(Base,EZSP,Nr1,Pr-nr=20,Offset=1,Path=2,WNr-Point=20,Pr=65,ON)” =11=]
使用的正则表达式: (?:(W-点=)(\d*)|(SST_P-Nr=)(\d*)|(WR/KE-Point=)(\d*)|(WNr-点=)( \d*))
到目前为止一切正常:Example
问题: 识别匹配的 word/digit 对并获取其组号。现在我循环遍历结果并丢弃空的子匹配。有更好或更有效的方法吗?
提前致谢。
尝试
Sub test()
Dim s As String
s = "3: CALL U(Base,EZSP,Nr1,Pr-nr=20,Offset=1,Path=2,WNr-Point=20,Pr=65,ON)"
Dim Regex As Object, m As Object
Set Regex = CreateObject("vbscript.regexp")
With Regex
.Global = True
.MultiLine = False
.IgnoreCase = True
.pattern = "(W-Point|WR/KE-Point|WNr-Point|SST_P-Nr)( *= *)(\d*)"
End With
If Regex.test(s) Then
Set m = Regex.Execute(s)(0).submatches
Debug.Print m(0), "'" & m(1) & "'", m(2)
End If
End Sub
更新:捕获 = 和任何空格