in reply to REgular expression to check the string that allows "a","b" and "c" to occur only once in any order.

Thanks for making me think about regex in yet another way!! Here is a easy to understand code
use strict; use warnings; my $str=""; for $str('aaa'..'ccc') { if($str=~m/^[abc]{3}$/) { if($str!~m/(.*a.*a)/) { if($str!~m/(.*b.*b)/) { if($str!~m/(.*c.*c)/) { print "\' $str \' is a Match\n"; } else { print "The string $str contains duplicated 'c'\n"; } } else { print "The string $str contains duplicated 'b'\n"; } } else { print "The string $str contains duplicated 'a'\n"; } } else { print "Either the string \'$str\' contains a character not bel +onging to the class [abc] or is more than 3 characters\n"; } }
which can be condensed as
use strict; use warnings; my $str=""; for $str('aaa'..'ccc') { if($str=~m/^[abc]{3}$/ && $str!~m/(.*a.*a)/ && $str !~ m/(.*b.*b)/ + && $str!~ m/(.*c.*c)/) { print "\' $str \' is a Match\n"; } }

UPDATE: I overlooked the complexity of the problem.Thanks to Punitha,merlyn,KurtSchwind for their inputs

The world is so big for any individual to conquer

  • Comment on Re: REgular expression to check the string that allows "a","b" and "c" to occur only once in any order.
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: REgular expression to check the string that allows "a","b" and "c" to occur only once in any order.
by KurtSchwind (Chaplain) on Dec 11, 2007 at 05:43 UTC

    Update: Nicely re-worked. Previously it didn't work but now it does. Nice job. I'm leaving my initial response here anyway, but it's no longer pertinent.

    That doesn't work because it incorrectly matches invalid strings.

    If you change

    my $str="abc";
    to
    my $str="aaa";
    You'll still get a match even though that's not supposed to be a valid string. You'll have to go a bit more complicated in the regex to exclude the false hits.

    Update: I was missing my 2nd code snip.
    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.