Reputation: 4122
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
Upvotes: 2
Views: 3148
Reputation: 93151
If your strings contain only the number with possible 0
padding, use hasPrefix
let str = "00234"
let match = !str.hasPrefix("0") // false
Upvotes: 1
Reputation: 1504
Use following regular expression to input the numbers starting from 1 to 9
"^[1-9][0-9]*$"
Upvotes: 1
Reputation: 726489
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.
Upvotes: 4