szabgab has asked for the wisdom of the Perl Monks concerning the following question:

In another post palindrome using regular expressions I asked about another ways to write catch a palindrome but my real interest is seeing examples of real world usage of extended regular expression. So if you have examples where you used any of the following constructs, I would appreciate seeing them.
(?=pattern) - zero-width positive look-ahead (?!pattern) - zero-width negative look-ahead (?<=pattern) - zero-width positive look-behind (?<!pattern) - zero-width negative look-behind (?{ code }) - zero-width assertion with embedded code (??{ code }) - postponed subexpression (?>pattern) - "independent" subexpression (?(condition)yes-pattern|no-pattern) - conditional expression (?(condition)yes-pattern)

Replies are listed 'Best First'.
Re: real world extended regex examples
by GrandFather (Saint) on Oct 12, 2006 at 14:22 UTC
Re: real world extended regex examples
by Fletch (Bishop) on Oct 12, 2006 at 14:20 UTC

    Install Regexp::Common and snoop around in its guts. There's a few of these therein.

Re: real world extended regex examples
by Eimi Metamorphoumai (Deacon) on Oct 12, 2006 at 15:44 UTC
    You might look at the rules in SpamAssassin, and the custom rules at, say http://www.rulesemporium.com/. Lots of complex patterns, particuarly the (?:posi|nega)tive look-(?:ahea|behin)d.

    As a simple example, consider something like /\bviagra\b/i. Now assume you want to match also "\/i@gr@" (very simple obfuscation). If you do /\b(?:\\\/|v)i[@a]gr[@a]\b/i, it doesn't actually work for what you want it to. That's because \b insists on a word boundery, and since \ and @ aren't word characters, they'll only match if adjacent to non-word characters. So what you really want is /(?<!\w)(?:\\\/|v)i[@a]gr[@a](?!\w)/i.

Re: real world extended regex examples
by johngg (Canon) on Oct 12, 2006 at 14:42 UTC
    I really like this neat combination of positive looks behind and ahead by davidrw. It immediately sprang to mind when I saw your post but it might not come up in a search as no mention is made in it of extended methods.

    Cheers,

    JohnGG

Re: real world extended regex examples
by blazar (Canon) on Oct 12, 2006 at 14:56 UTC

    On a slightly less serious basis than the other answers you got... the way I explain (how not to misspell) my nick.

Re: real world extended regex examples
by planetscape (Chancellor) on Oct 12, 2006 at 21:34 UTC