Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I'm struggling with capture of matched bits when using a quantifier. It seems that if I used a quantifier (e.g. * in this case) for a subpattern then only the final match is captured and returned by the regex. Here's some code that illustrates the problem. I'd like to capture the numbers 2 through 19, but only get 2, 3 and 19.
Using ActiveState perl 5.8.8 (don't ask) if it matters.
Many thanks for any assistance!
+++ cut here +++
$str = '12/22/2005 20 Notice of Agenda of Matters Scheduled for Hearin +g (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'; @matches = ($str =~ m/\(Related document\(s\)(\d+)\, (\d+)(?:\, (?:\[| +)(\d+)(?:\]|))*\)/i); print "dollar amp is $&\n"; print "found " . scalar(@matches) . " matches.\n"; foreach (@matches) { print " $_\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex capture and quantifiers
by kennethk (Abbot) on Apr 30, 2013 at 20:49 UTC | |
|
Re: regex capture and quantifiers
by AnomalousMonk (Archbishop) on Apr 30, 2013 at 23:32 UTC | |
|
Re: regex capture and quantifiers
by Laurent_R (Canon) on Apr 30, 2013 at 21:17 UTC | |
|
Re: regex capture and quantifiers
by 2teez (Vicar) on Apr 30, 2013 at 21:11 UTC | |
|
Re: regex capture and quantifiers
by peanut59 (Initiate) on Apr 30, 2013 at 20:18 UTC | |
by Laurent_R (Canon) on Apr 30, 2013 at 20:49 UTC |