John Weisz
John Weisz

Reputation: 31932

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

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.

Upvotes: 1

Views: 80

Answers (1)

Jonny 5
Jonny 5

Reputation: 12389

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.

Upvotes: 2

Related Questions