I have been struggling with a simple (or so I thought) regex for a day now and have only succeeded in confusing myself. Here's the scenario:
A user supplied string may contain 1 or more of the characters smtwhfa in any order. That's as easy as
$string =~ /^[smtwhfa]*$/
But this allows for duplicate characters and I need to be sure that no character is in the string more than once. For example:
swma is OK
smsa is NOT OK

I tried many variations of what I thought could work but to no avail. So, of course man perlre was my next stop. Then searches on Perl Monks and Google and then the camel book. The one thing that seemed to be the answer was a negative lookahead assertion but I don't really understand it. I'm not even sure if this the road I need to take.
Here is some of what I tried:
$string =~ /^[smtwhfa]*(?![smtwhfa])$/ $string =~ /^[s{1}m{1}t{1}w{1}h{1}f{1}a{1}]*$/ $string =~ /^([smtwhfa])(?!\1)*$/
And numerous other more embarrassing attempts.

I know there are other ways of doing this like this very ugly and inefficient example:
my $string = "smasm"; my @array = split(//, $string); my %hash; foreach(@array) { $hash{$_} ++; } foreach(keys %hash) { if($hash{$_} > 1) { print "ERROR\n"; last; } }
But I would really like to do this with a single regex so that it easily portable to other languages.

Any hints or examples that might lead me in the right direction will be greatly appreciated.

In reply to Regex (lookahead) Confusion by ChrisR

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.