RegEx: Grabbing values between quotation marks

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

    From Greg H. I was able to create this regex to suit my needs.

    I needed to match a specific value that was qualified by being inside quotes. It must be a full match, no partial matching could should trigger a hit

    e.g. "test" could not match for "test2".

    reg = r"""(['"])(%s)\1"""
    if re.search(reg%(needle), haystack, re.IGNORECASE):
        print "winning..."
    

    Hunter

    0 讨论(0)
  • 2020-11-22 02:46
    string = "\" foo bar\" \"loloo\""
    print re.findall(r'"(.*?)"',string)
    

    just try this out , works like a charm !!!

    \ indicates skip character

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

    For me worked this one:

    |([\'"])(.*?)\1|i
    

    I've used in a sentence like this one:

    preg_match_all('|([\'"])(.*?)\1|i', $cont, $matches);
    

    and it worked great.

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

    A very late answer, but like to answer

    (\"[\w\s]+\")
    

    http://regex101.com/r/cB0kB8/1

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

    If you're trying to find strings that only have a certain suffix, such as dot syntax, you can try this:

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

    Where .localized is the suffix.

    Example:

    print("this is something I need to return".localized + "so is this".localized + "but this is not")

    It will capture "this is something I need to return".localized and "so is this".localized but not "but this is not".

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

    Peculiarly, none of these answers produce a regex where the returned match is the text inside the quotes, which is what is asked for. MA-Madden tries but only gets the inside match as a captured group rather than the whole match. One way to actually do it would be :

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

    Examples for this can be seen in this demo https://regex101.com/r/Hbj8aP/1

    The key here is the the positive lookbehind at the start (the ?<= ) and the positive lookahead at the end (the ?=). The lookbehind is looking behind the current character to check for a quote, if found then start from there and then the lookahead is checking the character ahead for a quote and if found stop on that character. The lookbehind group (the ["']) is wrapped in brackets to create a group for whichever quote was found at the start, this is then used at the end lookahead (?=\1) to make sure it only stops when it finds the corresponding quote.

    The only other complication is that because the lookahead doesn't actually consume the end quote, it will be found again by the starting lookbehind which causes text between ending and starting quotes on the same line to be matched. Putting a word boundary on the opening quote (["']\b) helps with this, though ideally I'd like to move past the lookahead but I don't think that is possible. The bit allowing escaped characters in the middle I've taken directly from Adam's answer.

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