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]+")
Try this:
mystr.matches("^[1-9]\\d*$")
matches will return true only if entire string matches used regex so there is no need for ^ and $ :). Also try using "{}" key (or Ctrl+K) next time while creating answer to format selected code and you wont have to use tricks like \\\ to get two backslashes :)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).0 a legal input?I think that one should do the job: "([1-9]+[0-9]*)"
Cheers!
* after [0-9]. Try to avoid catastrophic backtracking
mystr.matches("[1-9]+")0a correct number?0, just0