(?P<NAME>...) vs (?<NAME>...)

Most Perl programmers using named captures in regex probably pick the (?<NAME>...) syntax, as that's what's displayed more prominently in the Perl documentation and tutorials.

However, Python does not support this syntax and uses (?P<NAME>...) instead (notice the extra P character). Incidentally, visual regex tool like kiki is built with Python and only support this syntax.

(?P<NAME>...) is also supported by Perl. So if you work with Python or use kiki, you might, like me, want to accustom yourself to using the P syntax.

PCRE (and thus PHP and other PCRE-using languages) supports both syntax. Komodo IDE's Rx toolkit support both. However, Ruby and .NET only support the non-P syntax. Well, that's how the real world works.

1 Comment

I believe the reason we support the Python variant is that we stole the idea from them.

I think the other libraries/languages stole it from Perl.

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays). My CPAN ID: SHARYANTO. I'm sedusedan on perlmonks. My twitter is stevenharyanto (but I don't tweet much). Follow me on github: sharyanto.