preg-match
How To Negate Regex [duplicate]
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.
Preg Match Empty String
You can use ^ to match beginning-of-line, and $ to match end-of-line, thus the regular expression ^$ would match an empty line/string. Your specific example (a line/string containing a single digit or being empty) would easiest be achieved with ^[0-9]?$.