Match a given sequence only if not between quotes, taking escaped quotes into consideration

88 views Asked by At

I use the below regular expression to only match a given character sequence if it is not surrounded by quotes - that is, if it is followed by an even number of quotes (using a positive lookahead) until the end of the string.

Say I want to match the word section only if it is not between quotes:

\bsection\b(?=[^"]*(?:"[^"]*"[^"]*)*$)

Working example on RegExr

How would I extend this to take escaped quotes into consideration? That is, if I insert a \" between the quotes in the linked example, the results stay the same.

1

There are 1 answers

1
Jonny 5 On BEST ANSWER

Using pcre could skip the quoted stuff:

(?s)".*?(?<!\\)"(*SKIP)(*F)|\bsection\b

In string regex pattern have to triple-escape the backslash, like \\\\ to match a literal backslash in the lookbehind. Or in a single quoted pattern double escaping it would be sufficient for this case.

$pattern = '/".*?(?<!\\\)"(*SKIP)(*F)|\bsection\b/s';

See test at regex101.