Friday 29 July 2016

preg match - How To Negate Regex





Possible Duplicate:
Regular expression to match string not containing a word?
How can I invert a regular expression in JavaScript?






Say I have the regex foo123. How do I match everything that is not foo123?


Answer




Use negative lookahead for this.



(?!foo123).+


matches any string except foo123



If you want to match empty string also, use (?!foo123).*



In your case (according to the comment) the required regex is (?!P[0-9]{1,}).+.




It matches P and 123, but not P123.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...