Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by lrl1997 (Novice)
on Feb 27, 2013 at 00:45 UTC ( #1020784=perlquestion: print w/ replies, xml ) Need Help??
lrl1997 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'd like to do a regex match using perl.

Say I want to search strings containing more than 2 but not more than 3 of a range of characters "ARKYGZX", any letters can be repeats (i.e. AAA, RR, XXK etc) or scattered in the string (i.e. AgoodApeople, XgoodpeopleK, etc), so that as an example, the following strings should be print out as output:

AAgoodpeople

AgoodApeople

XgoodpeopleK

but "ARKYgoodpeopleGZX" shouldn't.

my code is as:

if ($_ =~ /[ARKYGZX]{2,3}/){ print $_, "\n"; }

But I think this only works if any of the character are repeats, such as "AAgoodpeople", for strings AgoodpeopleA", or "AXgoodpeople" or "AgoodpeopleX", this code wouldn't work.

Any suggestions?

Thanks

Comment on match a range of characters multiple times but they might scattered in the string
Download Code
Re: match a range of characters multiple times but they might scattered in the string
by AnomalousMonk (Abbot) on Feb 27, 2013 at 01:03 UTC
    >perl -wMstrict -le "my $range = quotemeta 'ARKYGZX'; ;; my $rng = qr{ [$range] }xms; my $not_rng = qr{ [^$range] }xms; ;; for my $s (qw( AAA RR XXK AxA xAxxYxxRx A xAx ARKYGZX XxZxGxYxKxRxA AxAxAxA xYYYYx xRRRRRx RRRR )) { my $match = $s =~ m{ \A $not_rng* $rng (?: $not_rng* $rng){1,2} $not_rng* \z }xms; printf qq{%s match : '%s' \n}, $match ? ' ' : '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'

    Update: Apply quotemeta directly to  $range initialization, don't use  \Q \E

Re: match a range of characters multiple times but they might scattered in the string
by toolic (Chancellor) on Feb 27, 2013 at 01:10 UTC
    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

      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'
Re: match a range of characters multiple times but they might scattered in the string
by 7stud (Deacon) on Feb 27, 2013 at 02:00 UTC

    Say I want to search strings containing more than 2 but not more than 3

    Let's see....more than 2...not more than 3, ahhh..................uhmmm.....................(start over) ...ahhhh ..................................uhhmm.........................(1...2...3).............(3..2..1)...............uuhhhh, "What is some number in Normandy, but I just won $75k?"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020784]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-09-19 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (132 votes), past polls