RegEx: Grabbing values between quotation marks

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

    The RegEx of accepted answer returns the values including their sourrounding quotation marks: "Foo Bar" and "Another Value" as matches.

    Here are RegEx which return only the values between quotation marks (as the questioner was asking for):

    Double quotes only (use value of capture group #1):

    "(.*?[^\\])"

    Single quotes only (use value of capture group #1):

    '(.*?[^\\])'

    Both (use value of capture group #2):

    (["'])(.*?[^\\])\1

    -

    All support escaped and nested quotes.

    0 讨论(0)
  • 2020-11-22 02:43

    I've been using the following with great success:

    (["'])(?:(?=(\\?))\2.)*?\1
    

    It supports nested quotes as well.

    For those who want a deeper explanation of how this works, here's an explanation from user ephemient:

    ([""']) match a quote; ((?=(\\?))\2.) if backslash exists, gobble it, and whether or not that happens, match a character; *? match many times (non-greedily, as to not eat the closing quote); \1 match the same quote that was use for opening.

    0 讨论(0)
  • 2020-11-22 02:43

    Lets see two efficient ways that deal with escaped quotes. These patterns are not designed to be concise nor aesthetic, but to be efficient.

    These ways use the first character discrimination to quickly find quotes in the string without the cost of an alternation. (The idea is to discard quickly characters that are not quotes without to test the two branches of the alternation.)

    Content between quotes is described with an unrolled loop (instead of a repeated alternation) to be more efficient too: [^"\\]*(?:\\.[^"\\]*)*

    Obviously to deal with strings that haven't balanced quotes, you can use possessive quantifiers instead: [^"\\]*+(?:\\.[^"\\]*)*+ or a workaround to emulate them, to prevent too much backtracking. You can choose too that a quoted part can be an opening quote until the next (non-escaped) quote or the end of the string. In this case there is no need to use possessive quantifiers, you only need to make the last quote optional.

    Notice: sometimes quotes are not escaped with a backslash but by repeating the quote. In this case the content subpattern looks like this: [^"]*(?:""[^"]*)*

    The patterns avoid the use of a capture group and a backreference (I mean something like (["']).....\1) and use a simple alternation but with ["'] at the beginning, in factor.

    Perl like:

    ["'](?:(?<=")[^"\\]*(?s:\\.[^"\\]*)*"|(?<=')[^'\\]*(?s:\\.[^'\\]*)*')
    

    (note that (?s:...) is a syntactic sugar to switch on the dotall/singleline mode inside the non-capturing group. If this syntax is not supported you can easily switch this mode on for all the pattern or replace the dot with [\s\S])

    (The way this pattern is written is totally "hand-driven" and doesn't take account of eventual engine internal optimizations)

    ECMA script:

    (?=["'])(?:"[^"\\]*(?:\\[\s\S][^"\\]*)*"|'[^'\\]*(?:\\[\s\S][^'\\]*)*')
    

    POSIX extended:

    "[^"\\]*(\\(.|\n)[^"\\]*)*"|'[^'\\]*(\\(.|\n)[^'\\]*)*'
    

    or simply:

    "([^"\\]|\\.|\\\n)*"|'([^'\\]|\\.|\\\n)*'
    
    0 讨论(0)
  • 2020-11-22 02:43

    MORE ANSWERS! Here is the solution i used

    \"([^\"]*?icon[^\"]*?)\"

    TLDR;
    replace the word icon with what your looking for in said quotes and voila!


    The way this works is it looks for the keyword and doesn't care what else in between the quotes. EG:
    id="fb-icon"
    id="icon-close"
    id="large-icon-close"
    the regex looks for a quote mark "
    then it looks for any possible group of letters thats not "
    until it finds icon
    and any possible group of letters that is not "
    it then looks for a closing "

    0 讨论(0)
  • 2020-11-22 02:44

    I would go for:

    "([^"]*)"
    

    The [^"] is regex for any character except '"'
    The reason I use this over the non greedy many operator is that I have to keep looking that up just to make sure I get it correct.

    0 讨论(0)
  • 2020-11-22 02:44
    echo 'junk "Foo Bar" not empty one "" this "but this" and this neither' | sed 's/[^\"]*\"\([^\"]*\)\"[^\"]*/>\1</g'
    

    This will result in: >Foo Bar<><>but this<

    Here I showed the result string between ><'s for clarity, also using the non-greedy version with this sed command we first throw out the junk before and after that ""'s and then replace this with the part between the ""'s and surround this by ><'s.

    0 讨论(0)
提交回复
热议问题