Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Capture groups

by brian_d_foy (Abbot)
on Mar 05, 2008 at 00:51 UTC ( [id://672056]=note: print w/replies, xml ) Need Help??


in reply to Capture groups

Are you trying to use Perl 5.10 and the smart match operator? Here's the solution written with named captures, the nifty new 5.10 features that mean you never have to think about $1, $2, and so on. The names become keys in the new hash %+, and the matched text are the values:

#!/usr/local/bin/perl5.10.0 use 5.010; my $text = "Grand Canyon 70%"; $text ~~ / (?<name> .*\S ) \s+ (?<percent> \d\d% ) /x; use Data::Dumper; print Dumper( \%+ )

The output automatically labels your values:

$VAR1 = { 'percent' => '70%', 'name' => 'Grand Canyon' };

Here's the boring Perl 5.8 way:

#!/usr/bin/perl my $text = "Grand Canyon 70%"; $text =~ / ( .*\S ) \s+ ( \d\d% ) /x; print <<"HERE"; 1: $1 2: $2 HERE
--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://672056]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-23 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found