On Aug 22, Michel Blanc said:

>Jeff 'japhy/Marillion' Pinyan a écrit :
>> 
>
>> I think you mean "if a letter has already matched, it CAN'T repeat again
>> in the string" -- meaning, each character must be unique.
>
>Yes, that's right.
>
>> It's not a simple task without a complex regex assertion, (??{ ... }).
>> 
>> I can provide a solution, but I cannot guarantee it will be easy to
>> understand once explained.
>
>I already spend a lot of energy on your JAPHs :)
>Thank you for spending time on this.
>When you'll send me your solution, I'll come back and tell you if I
>understand what you did !

Someone else has already shown the general approach for ensuring a
unique-character string:

  sub unique_characters {
    $_[0] =~ /(.).*?\1/s ? 0 : 1
  }

We can extrapolate upon this idea and test afterward that the string
doesn't contain any unwanted characters:

  sub unique_char_set {
    my ($str, $chars) = @_;
    return 0 if $str =~ /[^\Q$chars\E]/;
    return !($str =~ /(.).*?\1/s);
  }

This returns false if there is an invalid character, or if there is a
doubled character.  Otherwise, it returns true.

You can do this using the (??{ ... }) assertion, like I suggested.  It is
more complex, and I'm not sure how it compares speed-wise.  And, when I
think of it, it is totally improper.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to