Extended Regular Expressions
Perl defines an extended syntax for regular expressions. The syntax is a pair of parentheses with a question mark as the first thing within the parentheses. The character after the question mark gives the function of the extension. The extensions are:
(?#text)A comment. The text is ignored.
(?:...)(?imsx-imsx:...)This groups things like
(...)but doesn’t make backreferences.(?=...)A zero-width positive lookahead assertion. For example,
/\w+(?=\t)/matches a word followed by a tab, without including the tab in$&.(?!...)A zero-width negative lookahead assertion. For example,
/foo(?!bar)/matches any occurrence offoothat isn’t followed bybar.(?<...)A zero-width positive lookbehind assertion. For example,
/(?<bad)boy/matches the wordboythat followsbad, without includingbadin$&. This works only for fixed-width lookbehind.(?{code})An experimental regular expression feature to evaluate any embedded Perl code. This evaluation always succeeds, and
codeis not interpolated.(?<!=...)A zero-width negative lookbehind assertion. For example,
/(?<!=bad)boy/matches any occurrence ofboythat doesn’t followbad. This works only for fixed-width lookbehind.(?>...)Matches the substring that the standalone pattern would match if anchored at the given position.
(?(condition)yes-pattern|no-pattern)(?(condition)yes-pattern)Matches a pattern determined by a condition.
conditionshould be either an integer, which is true if the pair of parentheses corresponding ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access