Validate increasing and decreasing numbers with regex

2

I need to create a regex for validating increasing and decreasing numbers as the example:

  

Repeated | growing | Decreasing
  000000 | 012345 | 987654
  111111 | 123456 | 543210
  [09:30:16] William Oliveira:

Repeat validation is in regex ^(\w)1\{5,}$

    
asked by anonymous 26.07.2016 / 19:02

1 answer

3

You could do the following: ^0*1*2*3*4*5*6*7*8*9*$ to detect increasing, ^9*8*7*6*5*4*3*2*1*0*$ to decreasing and ^0*|1*|2*|3*|4*|5*|6*|7*|8*|9*$ to repeated, eg:

import java.util.regex.Pattern;
class Main {
    public static void main(String args[]) {
        // match crescente
        System.out.println(Pattern.matches("^0*1*2*3*4*5*6*7*8*9*$", "1368"));
        System.out.println(Pattern.matches("^0*1*2*3*4*5*6*7*8*9*$", "9321"));

        // match decrescente
        System.out.println(Pattern.matches("^9*8*7*6*5*4*3*2*1*0*$", "1368"));
        System.out.println(Pattern.matches("^9*8*7*6*5*4*3*2*1*0*$", "9321"));

        // match repetidos
        System.out.println(Pattern.matches("^0*|1*|2*|3*|4*|5*|6*|7*|8*|9*$", "1111111111111"));
        System.out.println(Pattern.matches("^0*|1*|2*|3*|4*|5*|6*|7*|8*|9*$", "1111111111112"));
    }
}

that will get you back:

true
false

false
true

true
false
    
26.07.2016 / 19:40