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

#!/usr/bin/perl use strict; use diagnostics; use Data::Dumper; use Regexp::Common qw(balanced); my $re=$RE{balanced}{-parens=>'[]'}; # a valid string is: beginning, one or more [things] perhaps seperated + by whitespace, end foreach ( 'a [foo] [bar][quux] grmbl42', #1 ok 'a [foo] [bar]bla[quux] grmbl42', #2 nok: has random crap instead + of whitespace 'a [foo] [bar] grmbl42', #3 ok 'a [foo] grmbl42', #4 ok 'a [fo grmbl42', #5 nok: unbalanced 'a [fo]o] grmbl42', #6 nok: unbalanced 'a [fo[o] grmbl42', #7 nok: unbalanced 'a o] grmbl42', #8 nok: unbalanced 'a grmbl42', #9 nok: no [] at all ) { my $didit=/^ a # literal a \s* # optional whitespace (?: $re \s* )+ .* # something 42 # literal 42 $/x; print Dumper $_, $didit; };
Should fail #2 and #6, but doesn't. Why?

Replies are listed 'Best First'.
Re: Regexp::Common balanced brackets non-capture multiple occurances
by davidrw (Prior) on Jul 03, 2006 at 15:20 UTC
    If you add capturing you can see what it's doing ..
    my $didit=/^ a # literal a \s* # optional whitespace ((?: $re \s* )+) (.*) # something 42 # literal 42 $/x; print Dumper [$_, $didit?($1,$2):(undef,undef), $didit];
    For exampl, with #2 & #6 that gives:
    # 2 $VAR1 = [ 'a [foo] [bar]bla[quux] grmbl42', '[foo] [bar]', 'bla[quux] grmbl', '1' ]; # 6 $VAR1 = [ 'a [fo]o] grmbl42', '[fo]', 'o] grmbl', '1' ];
    The issue is with the "something" .* -- it just sucks up anything bad.