This is an interesting problem which can appear in certain cases (although not very often). A little searching around led me to many posts stating that there is no easy solution and the following easy solution:
^((?!my string).)*$
It works as follows: the matching string must contain zero or more characters which are not preceded (?! is the negative look behind operator) by the given string.
It is quite straight-forward, uses operators which are widely supported by regualar expression engines and works even if “my string” is at the end of the string we are trying to match – for reasons which are not entirely clear to me.
Obviously it is a hack and you shouldn’t use it if you can use a clearer way to indicate your intention, but it is a nifty tool to have in your toolbox for that one moment when you need it.
Read full article from hype-free: RegEx which matches strings not containing a substring
No comments:
Post a Comment