Wednesday, August 1, 2012

Regexp::Common : numbered captures, named captures

Regexp::Common - Provide commonly requested regular expressions - metacpan.org

I came across R::C through the chapter on Canned Regexes in the Perl Best Practices book [Link].

Don't get me wrong: I really appreciate R::C (i.e. this package)! Really! All due respect!

Under "-keep" results from R::C get returned as numbered captures.

If I figure that out correctly, R::C got (mainly) created and further developed before the dawn of named captures in Perl. And I guess, named captures never really made their way into R::C. (Pls let me know, if I'm wrong! I am glad to scrap this article, once I know better.)

Recently I wrote some code, where I parsed date+time strings using named captures. The regexes weren't really that fancy but rather precise.

Now I rewrote the code to make use of R::C resp. Regexp::Common::time. Pls get me right: I really like to specify the format the strftime way. But that's only half the cake. As mentioned I wasn't able to make use of named captures in order to access the parsed values then. It would be nice, to use the strftime specifiers for accessing the values through named captures by the same name, wouldn't it?!?

Maybe that's mainly stubborn me, because I don't like numbered captures any more, esp. if the regex is a little bigger and has the tendency to even grow.
Of course with R::C my code and esp. the date+time matching is far more flexible, but still with named captures and w/o R::C in this rather narrow context it is fare more readable and everything is in one place.

No comments: