2

I stumbled across a problem trying to perform a regex that captures everything between two quotation marks "". I noticed that sometimes there is a line break that occurs in between these quotation marks which breaks the regex.

Current regex that I am using: \"((?:(?![(]).)*)\"

This does a great job of capturing everything between quotation marks except if a line break occurs.

Any regex gurus know how to also allow for line breaks, this pattern has me stumped.

mrzasa
  • 22,895
  • 11
  • 56
  • 94
MaxB
  • 428
  • 1
  • 8
  • 24
  • 1
    ..or simply `flags=re.DOTALL` – Andrej Kesely Aug 13 '19 at 18:13
  • Possible duplicate of [How to use JavaScript regex over multiple lines?](https://stackoverflow.com/questions/1979884/how-to-use-javascript-regex-over-multiple-lines) – ggorlen Aug 13 '19 at 18:14
  • Looks like your regex is matching a quoted string but skipping a match if `(` is anywhere inside quoted string. Is that how you wanted? – anubhava Aug 13 '19 at 18:36

1 Answers1

6

Could you use simply:

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

Demo

Eg.

re.search(r'\"([^\"]*)\"', "\"a\nb\"").groups()
# Out[19]: ('a\nb',)
mrzasa
  • 22,895
  • 11
  • 56
  • 94