Write the shortest function that takes one string, and returns a regular expression that matches anything EXCEPT the input string, in parenthesis. Escape necessary characters like Perl quotemeta
.
Input:
helloworld
Output:
([^h]|h[^e]|he[^l]|hel[^l]|hell[^o]|hello[^w]|hellow[^o]|hellowo[^r]|hellowor[^l]|helloworl[^d])
Input:
*/
Output:
([^\*]|\*[^\/])
See also:
^(?!hello$)(.*)$
should work also (depending on the exact requirement - it matches anything which is not exactly "hello", i.e. "hello!" would match). – Howard Jul 11 '11 at 7:13