|P is for Practical|
Regex (lookahead) Confusionby ChrisR (Hermit)
|on Feb 05, 2004 at 20:43 UTC||Need Help??|
ChrisR has asked for the
wisdom of the Perl Monks concerning the following question:
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
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:
And numerous other more embarrassing attempts.
I know there are other ways of doing this like this very ugly and inefficient example:
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.