Here is an example that may work for the OP. It uses a similar approach as that found in perlretut (as suggested by the good monk, pancho) on page 22 of the tutorial. It allows multiple matches of various capturing subexpressions and keeps track of the number of matches for each of those subexpressions.
I just tested it for the admittedly simple case shown and it appears to do what is sought.
#!/user/bin/perl use strict; use warnings; my $text = "match other match not useful match not same match\n"; my @matches = ('(\b\w+\b)', '(\s)', '(\w+$)'); my $match_str; foreach my $identifyer (@matches) {$match_str = $identifyer . "|"}; my @word = (0,0,0); # declare word hash $text =~ m/ (\b\w+\b) (?{$word[0]++}) (\s) (?{$word[1]++}) (\w+$) (?{$word[2]++}) /gx; foreach my $i (0,1,2) { print "frequency of regex capture $matches[$i] is $word[$i]\n"; } exit(0);
Fore the input:
$text = "match other match not useful match not same match\n"the output is:
frequency of regex capture (\b\w+\b) is 8 frequency of regex capture (\s) is 8 frequency of regex capture (\w+$) is 1
The user has to specify the various capturing regex subexpressions and associate an element of the array @word with each of them. This array records each time it is matched by using the in-line regex code subpattern, (?{ }).
I think this does what the OP was looking for.
I'm not sure how robust it is (i.e., how flexible it is for example with nested capturing subexpressions, for alternating subexpressions, etc.
I am always challenged, especially, by alternating subexpressions.
In reply to Re^2: How can I access the number of repititions in a regex?
by ack
in thread How can I access the number of repititions in a regex?
by pat_mc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |