I need a regex for 5 digits in increasing order, like 12345, 24579, 34680, and so on.
0 comes after 9.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can try (as seen on rubular.com)
Explanation
^and$are the beginning and end of string anchors respectively\d{5}is the digit character class\drepeated exactly{5}times(?=...)is a positive lookahead?on each digit makes each optionalHow it works
\d{5}till the end of the stringregular-expressions.info
Generalizing the technique
Let’s say that we need to match strings that consists of:
[aeiou]Then the pattern is (as seen on rubular.com):
Again, the way it works is that:
(?=[aeiou]{1,3}$)Allowing repetition
If each digit can repeat, e.g.
11223is a match, then:?(zero-or-one) on each digit,*(zero-or-more repetition)That is, the pattern is (as seen on rubular.com):