Reputation: 111
I tried the following to match any number except 0 and 1 (say, 2 to 9999), but it does not seem to work as desired.
\d[0-9]?[0-9]?[^0-1]*
Upvotes: 4
Views: 25745
Reputation: 626861
You can match all numbers from 2 to 9999 using
\b(?![01]\b)\d{1,4}\b
Or (if you have individual strings)
^(?![01]$)\d{1,4}$
See demo
The (?!...)
is a negative lookahead that is used here to define exceptions.
More details
\b
- word boundary (if ^
is used - start of the string)(?![01]\b)
- a negative lookahead that fails the match if there is 0
or 1
([01]
is a character class that matches a single char from the set defined in the class) as a whole word (or string if $
is used instead of \b
)\d{1,4}
- 1, 2, 3 or 4 digits\b
- a trailing word boundary (no digit, letter or _
can appear immediately to the right, if there can be a letter or _
, replace with (?!\d)
).Upvotes: 10
Reputation: 1
This should match anything between 1 and infinity and match 1 or more digit from 1 to 9 trailing by 0 or more 0.
but it's wont work for 003 004 024:
[1-9]+[0-9]{0,}
Upvotes: -1
Reputation: 87203
Exclude 0
and 1
from character class in regex
.
[2-9]{1}\d{0,3}
This will match all the numbers which does not starts with 0
and 1
.
EDIT
To match all numbers except 0
and 1
[2-9]{1,4}
Upvotes: 10
Reputation: 1038
If you want to get all numbers between 2 and 9999, you'd need to check two cases, either if it's a length-1 number and then exclude cyphers 0 and 1, or allow everything, if the length is > 1. So the solutions would be something like:
(([2-9]{1})|([1-9]{1}[0-9]{1,}))
This does not allow 0 and 1, but allows 111, 322, 20, or 24.
Upvotes: 1