artist has asked for the wisdom of the Perl Monks concerning the following question:

Help me to build a regex. I want to validate the string against the duplicate occurence of the groups in the string. Number of members in group is 3. Members in the group can occur in any order. All members of the given group must be different. Every character is part of some or other group.

Some examples:
"abcdbca" => not a valid string (same groups: "abc" and "bca")
"abcba" => not a valid string (same groups: "abc" and "cba")
"abcdabe" =>a valid string

Thanks.

Replies are listed 'Best First'.
Re: Regex Help : Group in Strings
by BrowserUk (Patriarch) on Jan 06, 2005 at 23:57 UTC

    Update: Made a correction and added a variation.

    #! perl -slw use strict; my $re = qr[ (.) (?=(.)(.)) (?= .* (?: \1\2\3 | \1\3\2 | \2\1\3 | \2\3\1 | \3\1\2 | \3\2\1 ) .* $) ]x; while( <DATA> ) { chomp; print "'$_'", $_ !~ m[$re]g ? ' passed' : 'failed'; } __DATA__ abcdbca abcba abcdabe

    Ouput

    P:\test>test3 'abcdbca'failed 'abcba'failed 'abcdabe' passed

    A variation

    my $re = qr[ (.) (?=(.)(.)) (?= .* (?: \1 (?: \2\3 | \3\2 ) | \2 (?: \1\3 | \3\1 ) | \3 (?: \1\2 | \2\1 ) ) .* $) ]x;

    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
Re: Regex Help : Group in Strings
by dragonchild (Archbishop) on Jan 07, 2005 at 02:11 UTC
    A non-regex version.
    sub validator { my $string = shift; my @chunks = map { [ split '' ] } $string =~ /(?=(...))./g; @$_ = sort @$_ for @chunks; my %count; for (map { join '', @$_ } @chunks) { return if $count{$_}++; } return 1; }

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.