Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

BooK (2612)

BooK
  {book} {at} {cpan.org}
http://paris.mongueurs.net/
Yahoo! ID: philippe_bruhat (Add User, Send Message)

Obfuscation [plover.com]. Pink [axis-of-aevil.net]. HTTP::Proxy [cpan.org]. YEF [yapceurope.org]. Fishnet [perl.org]. Kapow [cpan.org]. Cog's [perl.org] bitch [cpan.org]. Invitation [perl.org]. White [perl.org] Camel [perl.org]. Nuff' said.

Journal of BooK (2612)

Sunday May 02, 2004
05:51 AM

Counting capturing parentheses

[ #18584 ]

This morning, with a little insight from Sheriff and TorgoX on #perl, I came up with the following subroutine:

sub captures {
    local $_ = shift;
    croak "$_ is not a compiled regexp" unless ref eq 'Regexp';
    my $n = 0;
    while( /\G(?=.)/gcs ) {
      /\G[^\\(]*/gc;     # ignore uninteresting stuff
      /\G(?:\\.)*/gc;    # ignore backslashed stuff
      /\G\(\?/gc;        # ignore special regex
      /\G\(/gc && $n++;  # a capturing ( !
    }
    $n;
}

The trick is to use compiled regular expressions. This way, I do not need to try to balance parentheses or look for the closing ones, because I know the regular expression I'm working on is correct.

I'll be using it in combination with Parse::Yapp (which I'm learing) to detect inconsistencies in statements such as:

regexp /session (closed|opened) for user (\w+)(?: by (.*))?/
       captures status, user, by;

I'm so proud of me I've put it on perlmonks as well...

Update 1: Hugo pointed at some shortcomings on perlmonks. Now I have to correct them, and keep this version and the one on perlmonks up-to-date.

Update 2: This is a more complicated problem than I thought, and I've moved the discussion here.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.