如何在SQL中提取特定的多个文本?

时间:2017-03-09 14:45:47

标签: mysql sql arrays excel-vba substring

我正在尝试从1列中提取特定文本,这似乎已连接了多个数据点。以下是显示在1行中的部分输出示例:

  

[{ “Q”:{ “与”:[{ “ID”: “1”, “标签”:[{ “标签识别”: “62”, “tagstr”:“的示例1 “},{” 标签识别 “:” 3" , “tagstr”: “的示例1 ”},{ “标签识别”: “65”, “tagstr”:“的示例1 “},{” 标签识别 “:” 71" , “tagstr”: “的示例1 ”}], “文本”: “的示例1 ”}], “隐藏” : “假”, “ID” 为 “1”, “questionalias”: “1”, “文本”: “的示例1 ”, “TTL”: “的示例1 ” }},

粗体文字是我想要提取的内容。在实践中,每个'Example1'都是从单词选项中选择的。因此我确切地知道我要找的是什么文字。我正在努力的是为输出创建一种方法来去除不需要的文本并返回关键词(大约8)

或者,如果有人在VBA中做了类似的事情,那也可以选择。

以前有人遇到过这个问题吗?

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式解析数据!真是太棒了!

有足够的(LeftMidRightInstr)函数来解析您的数据,对吧? < / p>

  

有些人在面对问题时会思考   “我知道,我会使用正则表达式。”   现在他们有两个问题。

我认为,您尝试绑定某些关键字(tagstr,text和ttl),请查看this

随意修改此表达式,请查看thisthat

在VBA中,从头开始没有正则表达式,因此将VBA引用添加到&#34; Microsoft VBScript正则表达式5.5&#34;

references

这是我的 exapmle 与您的数据:

Sub test()
    Dim Data As String
    Dim Re As RegExp
    Dim ReMatch As MatchCollection
    Dim CurrentMatch As Match


    Data = "[{" & Chr(34) & "q" & Chr(34) & ":{" & Chr(34) & "as" & Chr(34) & ":[{" & Chr(34) & "id" & Chr(34) & ":" & Chr(34) & "1" & Chr(34) & "," & Chr(34) & "tags" & Chr(34) & _
            ":[{" & Chr(34) & "tagid" & Chr(34) & ":" & Chr(34) & "62" & Chr(34) & "," & Chr(34) & "tagstr" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & "},{" & Chr(34) & "tagid" & Chr(34) & ":" & Chr(34) & "3" & Chr(34) & _
            "," & Chr(34) & "tagstr" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & "},{" & Chr(34) & "tagid" & Chr(34) & ":" & Chr(34) & "65" & Chr(34) & "," & Chr(34) & "tagstr" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & _
            "},{" & Chr(34) & "tagid" & Chr(34) & ":" & Chr(34) & "71" & Chr(34) & "," & Chr(34) & "tagstr" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & "}]," & Chr(34) & "text" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & _
            "}]," & Chr(34) & "hidden" & Chr(34) & ":" & Chr(34) & "false" & Chr(34) & "," & Chr(34) & "id" & Chr(34) & ":" & Chr(34) & "1" & Chr(34) & "," & Chr(34) & "questionalias" & Chr(34) & ":" & Chr(34) & "1" & Chr(34) & _
            "," & Chr(34) & "text" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & "," & Chr(34) & "ttl" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & "}},"

    Debug.Print "My data is:" & vbNewLine & Data

    Set Re = New RegExp
    Re.IgnoreCase = True
    Re.Global = True
    Re.MultiLine = True
    Re.Pattern = "(?=" & Chr(34) & "tagstr" & Chr(34) & "|" & Chr(34) & _
            "text" & Chr(34) & "|" & Chr(34) & "ttl" & Chr(34) & ")(?:" & Chr(34) & _
            "\w*" & Chr(34) & ":" & Chr(34) & "(.*?)" & Chr(34) & ")"

    Debug.Print "My pattern is:" & vbNewLine & Re.Pattern

    Set ReMatch = Re.Execute(Data)

    Debug.Print "Matched " & ReMatch.Count & " times!"

    For Each CurrentMatch In ReMatch
        Debug.Print "Capture " & CurrentMatch.SubMatches(0) & " in " & CurrentMatch.Value
    Next

End Sub

<强>输出:

output

没那么复杂吧? 毕竟,您可以使用标准字符串函数执行此操作..