Reputation: 15
I'm looking for a regex that will validate a number. One part is easy ^(\d{5,9})$ the string representing the number must be 5 to 9 digits.
Part 2 I don't know how: It must NOT start with 9999
How can I add that part?
Upvotes: 0
Views: 314
Reputation: 88
I would recommend that you use your programming language's normal string functions to extract the first four characters of the string and compare them to "9999". This will be more efficient than a negative lookahead assertion, and also easier to read.
I suppose there could be some special circumstances where it needs to conform to the regex format. If this is the case, then the other answers have what you need. But I think it's good to realize not everything involving pattern matching has to use a regular expression.
Upvotes: 0
Reputation: 74197
You want a negative-lookahead assertion, anchored at start of string:
Regex rx = new Regex( @"^(?!9999)\d{5,9}$" ) ;
Upvotes: 1
Reputation: 89547
This do the job:
^(?!9999)\d{5,9}$
(?!....)
is a negative lookahead and means "not followed by"
Upvotes: 2