in reply to Regex (lookahead) Confusion

...may contain 1 or more of the characters smtwhfa in any order.

That means you want '+' instead of '*' for the quantifier.

You can make sure there are no repetitions with a hash over the characters:

if ( $string =~ /^[smtwhfa]+$/ and length $string == do { my %hsh; @hsh{split //, $string} = (); keys %hsh; } ) { # everything's ok }
That uses the uniqueness of hash keys to enforce your requirement.

You can do that with regex lookahead by a negated match, $string !~ /([smtwhfa])(?=.*\1)/ after the match for the character class. That returns true if there is no match for a repeated character (untested).

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Regex (lookahead) Confusion
by ChrisR (Hermit) on Feb 06, 2004 at 13:24 UTC
    Thanks. You're right, using the * instead of the + allows the regex to return true if the string is blank. I did want to be sure that string contained at least one of the allowed characters.
    $string =~ /^(?:([smtwhfa])(?!.*\1))+$/