【问题标题】:VBA - Regex - String in a WordVBA - 正则表达式 - 单词中的字符串
【发布时间】:2018-09-16 13:38:25
【问题描述】:

我有这种类型的 json 响应:

{"success":true,"data":[{"guid":10101,"name":"name1","ispool":true, "添加日期":"2018-09-12T10:22:44","status":5,"lastactivity":"2018-09-13T03:15:06","templatechannels":[{"guid":10102,"name":"name2","iscampaign":false,,"ispool":true,"dateadded":"2018-09-12T10:22:44" ,"status":5,"lastactivity":"2018-09-13T03:15:06","templatechannels"},{........}]}]}

我想从这个回复中获取所有 guid...

guid的记录可能超过100条。我都想拥有。

【问题讨论】:

  • 你需要一个 JSON 解析器,而不是正则表达式。
  • “VBA JSON”的快速 google 发现 this library 看起来很容易使用。
  • 是的,但实际上这是一个字符串而不是 json。
  • 这就是 JSON 通常的样子,是的。

标签: json regex vba excel


【解决方案1】:

这是基于从单元格中读取字符串的正则表达式。如果还可以有您想要的guids,则将传递给guids?":(\d+[^,]) 的模式更改为。

Option Explicit
Public Sub test()
    Dim s As String, i As Long, arr()
    s = [A1]

    arr = GetMatches(s, "guid"":(\d+[^,])")
    For i = LBound(arr) To UBound(arr)
        Debug.Print arr(i)
    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

/
guid":(\d+[^,])
/
gm

guid": 匹配字符 guid": 的字面意思(区分大小写)

第一抓捕组(\d+[^,])

\d+ 匹配 digit (equal to [0-9]) + Quantifier — 匹配一次到无限次,尽可能多次,按需回馈(贪婪)

匹配下面列表中不存在的单个字符[^,] , 匹配字符,字面意思(区分大小写)

我提取第一组子匹配。

【讨论】:

  • 非常感谢,这正是我需要的。我真的很感谢你的努力。谢谢大佬。
  • 现在如何迭代?就像我将字符串声明为 Dim ptrn As String... 然后在下一行中为 ptrn = "" + allThings(k) + """:(\d+[^,])"""....它现在给我错误?
  • and than arr = GetMatches(rsp, ptrn) 将用于每次迭代。
  • 对不起,我不明白。正则表达式将检查字符串并将所有匹配项写入即时窗口 Ctrl + G。您将 [A1] 替换为字符串的源。
  • For k = 1 To 40 If allThings(k) "" Then Dim rsp As String, ptrn As String, i As Long, arr() rsp = hReq.ResponseText ptrn = "" + allThings(k) + """:(\d+[^,])""" arr = GetMatches(rsp, "guid"":(\d+[^,])") For i = LBound(arr) To UBound (arr) MsgBox arr(i) Next End If Next
【解决方案2】:

如果你有那个字符串,比如说,在 Excel 单元格 A1 中,你可以使用这个:

Dim arr As Variant
Dim iArr As Long

arr = Split(Range("A1").Value, "guid")
If UBound(arr, 1) > 0 Then
    For iArr = 1 To UBound(arr, 1)
        MsgBox Mid(arr(iArr), 3, InStr(arr(iArr), ",") - 3)
    Next
End If

在 OP 评论后编辑

要面对“guids”与“guid”的出现,您可以首先使用 Replace() 函数将所有“guids”更改为“guid”

arr = Split(Replace(Range("A1").Value, "guids", "guid"), "guid")

【讨论】:

  • 很好的解决方法,但是如果我有一些其他列名称相同但只有一个字符更像“guids”怎么办?它也会用 'guid' 读取 'guids'...在这种情况下该怎么办?
  • 我会考虑的。虽然您真的可以发布一个新问题来说明您的所有需求
  • 只要“guids”与“guid”是额外的问题,您可以先使用 Replace() 函数将所有“guids”更改为“guid”,然后继续使用此解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 2011-03-09
  • 2014-07-09
相关资源
最近更新 更多