Regex to match one or two quotes but not three in a row

Viewed 6938

For the life of me I can't figure this one out.

I need to search the following text, matching only the quotes in bold:

Don't match: """This is a python docstring"""

Match: " This is a regular string "

Match: "" ← That is an empty string

How can I do this with a regular expression?

Here's what I've tried:

Doesn't work:

(?!"")"(?<!"")

Close, but doesn't match double quotes.

Doesn't work:

"(?<!""")|(?!"")"(?<!"")|(?!""")"

I naively thought that I could add the alternates that I don't want but the logic ends up reversed. This one matches everything because all quotes match at least one of the alternates.

(Please note: I'm not running the code, so solutions around using __doc__ won't help, I'm just trying to find and replace in my code editor.)

2 Answers
Related