RegEx: Grabbing values between quotation marks

前端 未结 20 1533
暖寄归人
暖寄归人 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:32

    I liked Axeman's more expansive version, but had some trouble with it (it didn't match for example

    foo "string \\ string" bar
    

    or

    foo "string1"   bar   "string2"
    

    correctly, so I tried to fix it:

    # opening quote
    (["'])
       (
         # repeat (non-greedy, so we don't span multiple strings)
         (?:
           # anything, except not the opening quote, and not 
           # a backslash, which are handled separately.
           (?!\1)[^\\]
           |
           # consume any double backslash (unnecessary?)
           (?:\\\\)*       
           |
           # Allow backslash to escape characters
           \\.
         )*?
       )
    # same character as opening quote
    \1
    

提交回复
热议问题