参考这个question。
如何迭代每个键,如:
For k = 1 To 40
If allThings(k) <> "" Then
Dim rsp As String, ptrn As String, i As Long, arr()
rsp = [A1]
ptrn = "" + allThings(k) + """:(\d+[^,])""" 'guid,name,ispool,...
arr = GetMatches(rsp, ptrn)
For i = LBound(arr) To UBound(arr)
MsgBox arr(i)
Next
End If
Next
如下,仅用于将新搜索词连接到正则表达式中。
你可能想要改变正则表达式线(取决于像GetMatches(s, arr2(j) & """:(""?[A-Za-z0-9]+[^,])")
这样的字符
Option Explicit
Public Sub test2()
Dim rsp As String, i As Long, j As Long, arr2(), arr()
arr2 = Array("guid", "name", "ispool")
rsp = [A1]
For j = LBound(arr2) To UBound(arr2)
arr = GetMatches(rsp, arr2(j) & """:(""?\w+[^,])")
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i)
Next
Next
End Sub
Public Function GetMatches(ByVal inputString As String, ByVal sPattern As String) As Variant
Dim matches As Object, iMatch As Object, s As String, arrMatches(), i As Long
With CreateObject("vbscript.regexp")
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = sPattern
If .test(inputString) Then
Set matches = .Execute(inputString)
ReDim arrMatches(0 To matches.Count - 1)
For Each iMatch In matches
arrMatches(i) = iMatch.submatches.item(0)
i = i + 1
Next iMatch
Else
ReDim arrMatches(0)
arrMatches(0) = vbNullString
End If
End With
GetMatches = arrMatches
End Function
正则表达式 - 尝试here
name
的示例
name":("?\w+[^,])
/
gm
name": matches the characters name": literally (case sensitive)
第一捕获组("?\w+[^,])
"?
匹配字符“字面上(区分大小写)?
量词 - 匹配0到1次,尽可能多次,根据需要回馈(贪婪)
\w+
匹配任何单词字符(等于[a-zA-Z0-9_]
)+
量词 - 匹配一次和无限次,尽可能多次,根据需要回馈(贪婪)
匹配下面列表中不存在的单个字符[^,]
,
匹配字符,字面意思(区分大小写)
结果: