Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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 <>
Subscribe to The Perl Review

Comment on Re: Capture groups
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2015-10-13 09:25 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (298 votes), past polls