Alex Rivera | Logout

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

Asked 2009-08-29T18:57:25.077
28

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 quotes?

I'm thinking

^"((?<!\\)[^"]+)"

But my head spins, should that be a positive or a negative lookbehind? Or does it work at all?

How do I match any characters except a double-quote NOT preceded by a backslash?

Edit
Report

1 Answer

5

"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.

answered 2009-08-29T19:02:12.243

Your Answer