Is it possible to write a regular expression which will match any subset of a given set of characters
a1 ... an
?
I.e. it should match any string whe
This doesn't really qualify for the language-agnostic
tag, but...
^(?:(?!\1)a1()|(?!\2)a2()|...|(?!\n)an())*$
see a demo on ideone.com
The first time an element is matched, it gets "checked off" by the capturing group following it. Because the group has now participated in the match, a negative lookahead for its corresponding backreference (e.g., (?!\1)
) will never match again, even though the group only captured an empty string. This is an undocumented feature that is nevertheless supported in many flavors, including Java, .NET, Perl, Python, and Ruby.
This solution also requires support for forward references (i.e., a reference to a given capturing group (\1
) appearing in the regex before the group itself). This seems to be a little less widely supported than the empty-groups gimmick.