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

Can anybody help to have a regular expression that checks that a,b and c occur in a string in any order.
SO, the valid strings are abc, bca, cab, cba, bac, acb...
Invalid strings are aab, abbc, acc etc.

Comment on
REgular expression to check the string that allows "a","b" and "c" to occur only once in any order.
Replies are listed 'Best First'.

m/
(?=^[^a]*a[^a]*$) ## One a and no other a's
(?=^[^b]*b[^b]*$) ## One b and no other b's
(?=^[^c]*c[^c]*$) ## One c and no other c's
^.{3}$ ## Exactly three chars
/x and print "$_: matched" for 'aaa'..'ccc';;
abc: matched
acb: matched
bac: matched
bca: matched
cab: matched
cba: matched

True. The OP's spec wasn't clear as to whether or not the strings could contain other characters (or were limited to only those in the specified character set), or if a, b, and c were really characters and not stand-ins for longer strings. I went with Occam's razor and took the OP literally. (Update: Since the list of invalid strings contained only a, b, and c; I assumed if other characters were possible there would be an example of it in the invalid list. ikegami is right, though - this was probably more of an assumption than I first thought.)

This approach could also be problematic if the character set is large and/or if the wanted strings were significantly longer, since the number of permutations would rapidly increase. In that case, a RE-based approach may be better.

I did not spend much time on thinking of a regex as that would have been hairy, though I liked BrowserUk's regex (which could get unwieldy if needs to be modified).

Below is the version which checks for overlapping strings ("abcXa" is ok, "abca" not). As it is, the program below uses &A::C::permutations. Feel free to exercise any of the other two modules. Basically, the big idea is ...

I'm not sure whether this is in the rules but it does work. The code below sorts the data before matching. This makes the match fairly trivial since you need to look for the string abc without a an extra "a" or "c".

.oO, however, that this allows cases such as when @testdata contains aabc. IMO that's NOT quite1 (strictly) prohibited by the (very loose) specs from OP:

Can anybody help to have a regular expression that checks that a,b and c occur in a string in any order. SO, the valid strings are abc, bca, cab, cba, bac, acb... Invalid strings are aab, abbc, acc etc.

BUT the presence of abbc in the list of invalid strings suggests that isha may want to requires that each permitted letter occur only once. (Others may well infer differently. As noted, I consider the spec to be imprecise... as is, ISTM, the general case when specs are written by example.)

1. because, in this case, while the initial "a" is doubled the $test does include an instance of each acceptable character; in other words, ISTM that aabc is NOT - strictly - prohibited in the sample of invalid strings, aab because aab can be read as prohibiting only an incomplete set of the chars a, b and c, while the invalid string abbc does not strictly prohibit having the lead character doubled. Am I being pedantic or fuzzy-brained?

I do find my own observations to fall short of precision+clarity. Maybe it's too early in the AM... :-)