#!/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); #### frequency of regex capture (\b\w+\b) is 8 frequency of regex capture (\s) is 8 frequency of regex capture (\w+$) is 1