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.

ack Albuquerque, NM

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.