in reply to How can I access the number of repititions in a regex?
I don't know of a special variable for that but you can get the number of matches the following way
my @matches; .... while(<>) { my @matches = $_ =~ m/$string/g; my $repetition = @matches; print "$string was encountered $repetition times.\n" if($repetitio +n > 0); }
As for your second question, you are not closing the code tag correctly. You are using '\' where it should be '/'. So the closing tag should read </code>
update added the g modifier to the regexp. Thanks wfsp and Fletch for alerting me it was missing. It not being there was a typo. I tested it with the code shown bellow, but when adapting the test code to the block shown on the OP somehow the 'g' was skiped.
use strict; use warnings; my $text = "match other match not useful match sample word"; my $string = "match"; my @matches = $text =~ m/$string/g; my $count = @matches; print $count;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How can I access the number of repititions in a regex?
by ack (Deacon) on Mar 11, 2008 at 17:50 UTC | |
|
Re^2: How can I access the number of repititions in a regex?
by pat_mc (Pilgrim) on Mar 11, 2008 at 13:06 UTC | |
by Fletch (Bishop) on Mar 11, 2008 at 13:16 UTC | |
by ww (Archbishop) on Mar 11, 2008 at 14:23 UTC | |
by pat_mc (Pilgrim) on Mar 11, 2008 at 13:35 UTC | |
by olus (Curate) on Mar 11, 2008 at 14:42 UTC | |
by Anonymous Monk on Mar 11, 2008 at 14:14 UTC | |
by thundergnat (Deacon) on Mar 11, 2008 at 20:12 UTC |