charliek
charliek

Reputation: 121

Regex repeating numbers and sequences

Another Regex question. Have spend ages trawling through StackOverflow with no joy.

I need regexs (regexai?) for the following:

I don't expect one expression to fit all, guessing it'll probably be 2/3 required.

Cheers

Upvotes: 0

Views: 608

Answers (1)

npinti
npinti

Reputation: 52185

The only requirement, in my opinion which can be solved with a regular expression is the first, with an expression such as this: ((\d)\2){4}. This will attempt to match a digit and the same digit 4 times (it will look for 4 pairs).

The other requirements, such as checking if a digit is one less than the one the follows it and the last one cannot, to my knowledge be solved with a regular expression.

My recommendation would be to have a method which checks for each requirement and yield a boolean value denoting failure or success. This way at least you will have an idea of what you are doing and would be in a position to maintain the solution should one day the requirements change.

Long story short, what you are after can be achieved through a simple loop and some numerical checks.

Upvotes: 2

Related Questions