我會嘗試在VBA Excel中學習一些正則表達式。 如果您不提供RegEx代碼,則無法回答。 在VBA中,圖案確實匹配「尺寸34」和「蒙大拿356」。 MatchCollection數組中的第一個和第二個位置。難道你只能返回第一場比賽嗎?
'***/update/ 我用它作爲測試函數。
Function RegExpTest(patrn As String, strTest As String) As Variant
Dim regex As New VBScript_RegExp_55.RegExp
Dim Match As Match, Matches As MatchCollection
Dim cnt As Integer, cmb() As Variant
If patrn <> "" Then
With regex
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = patrn
End With
If regex.test(strTest) Then
Set Matches = regex.Execute(strTest)
cnt = Matches.Count
ReDim cmb((cnt * 3) - 1)
Dim i As Integer: i = 0
For Each Match In Matches
cmb(i) = " m:" & Match.Value & ","
i = i + 1
cmb(i) = "i:" & Match.FirstIndex & ","
i = i + 1
cmb(i) = "c:" & Match.Length & " |"
i = i + 1
' cmb(i) = "sub:" & Match.SubMatches.Count & "|"
' i = i + 1
Next
RegExpTest = Join(cmb)
Else
RegExpTest = 0
End If
End If
Set regex = Nothing
End Function
您是否還有其他想要捕捉什麼的例子,所以我們可以概括正則表達式?多個正則表達式可以捕獲「蒙大拿356」。 – Till
我會編輯...我的例子不合適... – user2348235