Regex - find anagrams and sub-anagrams

HghlnDR picture HghlnDR · Jan 28, 2013 · Viewed 8.4k times · Source

I have a pool of characters and I want to match all the words which are anagrams of those chars or of a subset of those chars using a regular expression.

Example: given the string "ACNE" the regex should give me these results:

  • ACNE [T]
  • CENA [T]
  • CAN [T]
  • CAAN [F]
  • CANEN [F]

I've tried this solution /b[acne]{1,4}/b but it accepts multiple repetitions of single chars. What can I do to take each char at most one time?

Answer

John Dvorak picture John Dvorak · Jan 28, 2013

The sub-anagrams of the word "acne" are the words that

  • consist only of the letters acne
  • do not contain a more than once
  • do not contain c more than once
  • do not contain n more than once
  • do not contain e more than once

Compiling this into a regex:

^(?!.*a.*a)(?!.*c.*c)(?!.*n.*n)(?!.*e.*e)[acne]*$

Test: regexpal

Alternatively, since "acne" does not contain any letter more than once, the sub-anagrams of the word "acne" are the words that

  • consist only of the letters acne
  • do not contain any letter more than once.

Compiling this into a regex:

^(?!.*(.).*\1)[acne]*$

Test: regexpal

Note: the sub-anagrams of the word "magmoid" can be matched as

^(?!.*([agoid]).*\1)(?!(.*m){3})[magoid]*$

(do not contain any of agoid more than once, and do not contain m more than twice)