in reply to Re^2: How can I access the number of repititions in a regex?
in thread How can I access the number of repititions in a regex?
The first solution that occurred to me is shown in the following code. Note that I considered splitting the input text on spaces, and that may not be a solution for you depending on your actual input and the patterns you are looking for.
use strict; use warnings; use Data::Dumper; my $text = "match other match not useful match sample word"; my $string1 = "match"; my $string2 = "not"; my %repetitions; map {$repetitions{$_}++;} grep /$string1|$string2/, split / /, $text; print Dumper(\%repetitions); ### Outputs: $VAR1 = { 'match' => 3, 'not' => 1 };
|
|---|