[tpm] problems with what regex returns ... in context ?
Fulko Hew
fulko.hew at gmail.com
Fri May 10 07:23:17 PDT 2013
Responding to my own post...
I just discovered that I can test the defined-ness
of $+ (the 'last' extraction) because I wasn't sure
if $1 $2... would be _also_ defined, if I specified a list to return into.
hmm, yes, $n are populated regardless of the left hand side
Thanks
On Fri, May 10, 2013 at 10:16 AM, Mike Stok <mike at stok.ca> wrote:
> My perl is getting a little rusty, but the first thing I thought of was to
> check the defined-ness of $1 e.g.
>
> my $line = 'this is stuff in here';
> my @patterns = qw / stuff (stuff) stuff/;
> foreach $regex (@patterns) {
> my @capture = ();
> print "regex is '$regex' ... ";
> if ((@capture = ($line =~ m/$regex/)) && defined $1) {
> print "captured " . scalar @capture . " items \n";
> my $i = 0;
> foreach my $item (@capture) {
> print " capture[$i]: '$item'\n";
> $i++;
> }
> } else {
> print "nothing captured\n";
> }
> }
>
> I'm sure there's some problem with that though...
>
> Mike
>
> On 2013-05-10, at 10:06 AM, Fulko Hew <fulko.hew at gmail.com> wrote:
>
> my $line = 'this is stuff in here';
> my @patterns = qw / stuff (stuff) /;
> foreach $regex (@patterns) {
> my @capture = ();
> print "regex is '$regex' ... ";
> if (@capture = ($line =~ m/$regex/)) {
> print "captured " . scalar @capture . " items \n";
> my $i = 0;
> foreach my $item (@capture) {
> print " capture[$i]: '$item'\n";
> $i++;
> }
> } else {
> print "nothing captured\n";
> }
> }
>
>
> --
>
> Mike Stok <mike at stok.ca>
> http://www.stok.ca/~mike/
>
> The "`Stok' disclaimers" apply.
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20130510/88ec312d/attachment-0001.html>
More information about the toronto-pm
mailing list