What's the regex to match anything except a double quote not preceded by a backslash?

后端 未结 2 1197
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 04:32

In other words, I have a string like:

\"anything, escaped double-quotes: \\\", yep\" anything here NOT to be matched.

How do I match everything inside the qu

相关标签:
2条回答
  • 2020-12-06 05:15

    No lookbehind necessary:

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

    So: match quotes, and inside them: every character except a quote ([^"]) or an escaped quote (\\"), arbitrarily many times (*).

    0 讨论(0)
  • 2020-12-06 05:17

    "Not preceded by" translates directly to "negative lookbehind", so you'd want (?<!\\)".

    Though here's a question that may ruin your day: what about the string "foo\\"? That is, a double-quote preceded by two backslashes, where in most escaping syntaxes we would be wanting to negate the special meaning of the second backslash by preceding it with the first.

    That sort of thing is kind of why regexes aren't a substitute for parsers.

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