in reply to match a range of characters multiple times but they might scattered in the string

Count the number of matches using m//g:
use warnings; use strict; while (<DATA>) { my @ms = /[ARKYGZX]/g; print if @ms == 2 or @ms == 3; } __DATA__ AAgoodpeople AgoodApeople XgoodpeopleK ARKYgoodpeopleGZX goodpeopleGZX

Prints:

AAgoodpeople AgoodApeople XgoodpeopleK goodpeopleGZX

Replies are listed 'Best First'.
Re^2: match a range of characters multiple times but they might scattered in the string
by AnomalousMonk (Archbishop) on Feb 27, 2013 at 01:23 UTC

    Could also count using  tr/// (wish I'd thought of that!).

    >perl -wMstrict -le "for my $s (qw( AAA RR XXK AxA xAxxYxxRx A xAx ARKYGZX XxZxGxYxKxRxA AxAxAxA xYYYYx xRRRRRx RRRR )) { my $n = $s =~ tr{ARKYGZX}{}; printf qq{%s match : '%s' \n}, ($n == 2 || $n == 3) ? ' ' : 'NO', $s; } " match : 'AAA' match : 'RR' match : 'XXK' match : 'AxA' match : 'xAxxYxxRx' NO match : 'A' NO match : 'xAx' NO match : 'ARKYGZX' NO match : 'XxZxGxYxKxRxA' NO match : 'AxAxAxA' NO match : 'xYYYYx' NO match : 'xRRRRRx' NO match : 'RRRR'