Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: regex capture and quantifiers

by kennethk (Abbot)
on Apr 30, 2013 at 20:49 UTC ( #1031473=note: print w/ replies, xml ) Need Help??


in reply to regex capture and quantifiers

When you have Capture groups with Quantifiers, only the last match is returned. For example,

use strict; use warnings; $_ = '1,2,3,4,5'; print /(,?\d)+/;
will output
,5
The solution for the task your shown here (IMHO) is to capture the entire series of numbers, then split on commas:
use strict; use warnings; my $str = '12/22/2005 20 Notice of Agenda of Matters Scheduled for Hea +ring (related document(s)2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, + 16, [17], 18, 19) Filed by Fubar, Inc.. Hearing scheduled for 4/11/2013 at 11:30 AM'; my ($series) = $str =~ m/\(Related document\(s\)([\]\[\d, ]+)\)/i; my @matches = split /,\s*/, $series; s/\[|\]//g for @matches; print "dollar amp is $&\n"; print "found " . scalar(@matches) . " matches.\n"; foreach (@matches) { print " $_\n"; }

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: regex capture and quantifiers
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2015-07-05 20:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls