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;
In reply to Re: How can I access the number of repititions in a regex?
by olus
in thread How can I access the number of repititions in a regex?
by pat_mc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |