I need to be able to match a series of numbers, (any number between 1
and 9
), with as many different digits as the user enters but no repetition.
123456789 -> match
1223 -> no match
In effect, the number must be between 1 and 9 digits long, containing only numbers, and not repeat any digit.
How would I do this using regex?
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
Python Regex Metacharacters [0-9] matches any single decimal digit character—any character between '0' and '9' , inclusive. The full expression [0-9][0-9][0-9] matches any sequence of three decimal digit characters. In this case, s matches because it contains three consecutive decimal digit characters, '123' .
$ means "Match the end of the string" (the position after the last character in the string).
match(/(\d{5})/g);
Something like below should work:
(?!.*([1-9]).*\1)^[1-9]{1,9}$
(?!.*([1-9]).*\1)
- negative look ahead checking if the digits don't repeat.
Sample matches: http://regexr.com?2trr6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With