RegEx: Grabbing values between quotation marks

前端 未结 20 1226
暖寄归人
暖寄归人 2020-11-22 02:13

I have a value like this:

\"Foo Bar\" \"Another Value\" something else

What regex will return the

相关标签:
20条回答
  • 2020-11-22 02:55

    In general, the following regular expression fragment is what you are looking for:

    "(.*?)"
    

    This uses the non-greedy *? operator to capture everything up to but not including the next double quote. Then, you use a language-specific mechanism to extract the matched text.

    In Python, you could do:

    >>> import re
    >>> string = '"Foo Bar" "Another Value"'
    >>> print re.findall(r'"(.*?)"', string)
    ['Foo Bar', 'Another Value']
    
    0 讨论(0)
  • 2020-11-22 02:55

    A supplementary answer for the subset of Microsoft VBA coders only one uses the library Microsoft VBScript Regular Expressions 5.5 and this gives the following code

    Sub TestRegularExpression()
    
        Dim oRE As VBScript_RegExp_55.RegExp    '* Tools->References: Microsoft VBScript Regular Expressions 5.5
        Set oRE = New VBScript_RegExp_55.RegExp
    
        oRE.Pattern = """([^""]*)"""
    
    
        oRE.Global = True
    
        Dim sTest As String
        sTest = """Foo Bar"" ""Another Value"" something else"
    
        Debug.Assert oRE.test(sTest)
    
        Dim oMatchCol As VBScript_RegExp_55.MatchCollection
        Set oMatchCol = oRE.Execute(sTest)
        Debug.Assert oMatchCol.Count = 2
    
        Dim oMatch As Match
        For Each oMatch In oMatchCol
            Debug.Print oMatch.SubMatches(0)
    
        Next oMatch
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题