Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use regex to match a series of numbers 1-9, with no repetition

Tags:

c#

regex

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?

like image 483
Doug S. Avatar asked May 27 '11 01:05

Doug S.


People also ask

How do you match a sequence in 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).

What does the 0 9 match in a regular expression in Python?

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' .

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How does regex Match 5 digits?

match(/(\d{5})/g);


1 Answers

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

like image 129
manojlds Avatar answered Oct 20 '22 15:10

manojlds