user2636197 user2636197 - 3 months ago 16
iOS Question

Swift ios regex to only allow postive numbers 1 and up

I am trying to make a regex that only allows positive numbers starting from 1 and up
E.g.:

1 // pass
23 // pass
023 // fail
00234 // fail
2340 // pass


So far I have this
"^0[0-9].*$"
but this will only allow numbers starting with 0 which I dont want.

So how can I make a regex that only supports positive numbers/integers

Answer

What I mean is starting from 1 and up

Regex for a decimal number that doe not start in 0 is as follows:

^[1-9][0-9]*$

Note that this regex will match sequences of digits that could not be represented as built-in numeric types in Swift.