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(?=[^"]*(?:"[^"]*"[^"]*)*$)
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.
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.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다