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 of foo that isn’t followed by bar.

(?<...)

A zero-width positive lookbehind assertion. For example, /(?<bad)boy/ matches the word boy that follows bad, without including bad in $&. This works only for fixed-width lookbehind.

(?{ code })

An experimental regular expression feature to evaluate any embedded Perl code. This evaluation always succeeds, and code is not interpolated.

(?<!=...)

A zero-width negative lookbehind assertion. For example, /(?<!=bad)boy/ matches any occurrence of boy that doesn’t follow bad. 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. condition should be either an integer, which is true if the pair of parentheses corresponding ...

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.