Reputation: 474
Hello guys i want to write a regex to check if a value has only numbers but 0 not to be in first position . for example the value 10 is correct but the value 01 is wrong.
so far i have this mystr.matches("[123456789]+")
Upvotes: 1
Views: 152
Reputation: 124215
All answers here will validate numbers like 1, 2, 3, ..., 10, 11, ... but in case you want to also include simple 0
but not 00
, 01
and so on you can use
mystr.matches("0|[1-9][0-9]*")
Upvotes: 2
Reputation: 85779
Proposed solution:
mystr.matches("[1-9]\\d*")
Explanation:
[1-9]
in the beginning to check if the first digit is between 1 and 9.\\d*
to look for any digit (form 0 to 9).Upvotes: 2