user1995272
user1995272

Reputation: 15

Regex: How to match all numbers just not one?

This should be so easy to do however I have been searching online and trying different patterns on http://gskinner.com/RegExr/ with no success!

I need to match all numbers and numbers only (from the beginning) except 1 or anything starting with a leading 0 so these would match

2
222
1234567

and these would not:

01
1
someword

Your help would be much appreciated! Thank you.

Upvotes: 1

Views: 71

Answers (5)

exussum
exussum

Reputation: 18550

^((?:[2-9][0-9]*)|(?:1[0-9]+))$

would work, Spiting each case

Example http://regex101.com/r/wW9jQ7

Upvotes: 3

stark
stark

Reputation: 13189

Single digits are special, so:

(^[2-9]$|^[1-9][0-9]+$)

Upvotes: 0

brandonscript
brandonscript

Reputation: 72855

Simplest way with no negation and no lookarounds:

^([2-9][0-9]*|[1-9][0-9]+)$

Working example: http://regex101.com/r/xZ2zC5

Upvotes: 0

Bohemian
Bohemian

Reputation: 424983

This is about the shortest I can think of:

^(?!1$)[^0]\d*$

Upvotes: 0

Niels Keurentjes
Niels Keurentjes

Reputation: 41958

(\b(([2-9]\d*)|(1\d+)))

Test case.

Upvotes: 5

Related Questions