Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Capture groups

by brian_d_foy (Abbot)
on Mar 05, 2008 at 00:51 UTC ( #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
Node Status?
node history
Node Type: note [id://672056]
help
Chatterbox?
[choroba]: The documentation just says the second argument should be "characters"

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2018-02-22 17:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (295 votes). Check out past polls.

    Notices?