... this won't work for all cases though, if you have two matching words in the neighbourhood of the same (currency|foreign exchange), just one will be counted. For exemple in "Currency revenue sales growth", you'll just get "revenue" ...

The following works for overlapping matches. It also needs 5.10+ because in addition to (?|pattern), it uses (*FAIL) from the Special Backtracking Control Verbs introduced in that version. The variation that only counts occurrences may be a little faster.

use 5.010; # for (?|pattern) and (*FAIL) use warnings FATAL => 'all' ; use strict; # factored regexes my $r_s_g = qr{ (?i) (?> \b (?: revenues? | sales | growth ) \b) }x +ms; my $c_fe = qr{ (?i) (?> \b (?: currency | foreign \W exchange) \b) }x +ms; my $word = qr{ \b \w+ \W+ (?: \b | \Z) }xms; my $max_between = 4; my $near = qr{ (?:$word){0,$max_between}? }xms; # the test text my $s = <<EOT; Currency revenue and sales growth in foreign exchange Sales and Revenues EOT print qq{[[$s]] \n\n}; # extract matches 'in context' our @matches; use re 'eval'; $s =~ m{ (?= (?| ($r_s_g) \W+ $near ($c_fe) | ($c_fe) \W+ $near ($r_s_g)) (?{ push @matches, [ $1, $2 ] }) (*FAIL) ) }xmsg; print qq{'$_->[0]' ... '$_->[1]' \n} for @matches; print qq{\n}; # just count matches our $n_matches; $s =~ m{ (?= (?| $r_s_g \W+ $near $c_fe | $c_fe \W+ $near $r_s_g) (?{ ++$n_matches }) (*FAIL) ) }xmsg; print qq{$n_matches matches \n};

Output:

[[Currency revenue and sales growth in foreign exchange Sales and Revenues ]] 'Currency' ... 'revenue' 'Currency' ... 'sales' 'Currency' ... 'growth' 'revenue' ... 'foreign exchange' 'sales' ... 'foreign exchange' 'growth' ... 'foreign exchange' 'foreign exchange' ... 'Sales' 'foreign exchange' ... 'Revenues' 8 matches

In reply to Re^2: Problems counting regex matches by AnomalousMonk
in thread Problems counting regex matches by elcilorien

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.