php - Match a given sequence only if not between quotes, taking escaped quotes into consideration -
i use below regular expression match given character sequence if not surrounded quotes - is, if followed number of quotes (using positive lookahead) until end of string.
say want match word section
if not between quotes:
\bsection\b(?=[^"]*(?:"[^"]*"[^"]*)*$)
how extend take escaped quotes consideration? is, if insert \"
between quotes in linked example, results stay same.
using pcre skip quoted stuff:
(?s)".*?(?<!\\)"(*skip)(*f)|\bsection\b
in string regex pattern have triple-escape backslash, \\\\
match literal backslash in lookbehind. or in single quoted pattern double escaping sufficient case.
$pattern = '/".*?(?<!\\\)"(*skip)(*f)|\bsection\b/s';
see test @ regex101.
Comments
Post a Comment