5
1
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:
Voting to close as unclear, because the examples do not do what the question says. – mbomb007 – 2018-02-01T15:14:29.680
I would have thought that that could be done more concisely in regex. – Peter Olson – 2011-07-11T05:47:35.193
The resulting regexes are somehow strange. E.g. The first one will match "x" or "hx" but not "xx". This is not "anything except the input". What do you really want to match? – Howard – 2011-07-11T06:12:10.460
2Or
^(?!hello$)(.*)$
should work also (depending on the exact requirement - it matches anything which is not exactly "hello", i.e. "hello!" would match). – Howard – 2011-07-11T07:13:01.510actually the regex I wrote had a problem. But I think there's a way to do that – JBernardo – 2011-07-11T07:18:18.343
@Howard I think that's exactly what I was trying to do – JBernardo – 2011-07-11T07:20:05.690
1Your example output for 'helloword' doesn't match the string 'h' or '' – Rob – 2011-07-20T02:15:41.927