Validate increasing and decreasing numbers with regex

Asked

Viewed 194 times

2

I need to create a regex for increasing and decreasing number validation as an example:

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

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

  • 2

    Honestly I didn’t understand any of your question. Edit and provide better explanation, otherwise it becomes complicated to help.

  • 1

    That fellow, just make it a little clearer, I couldn’t quite understand it either

1 answer

3

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

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 bring you back:

true
false

false
true

true
false
  • Thank you, Billy helped me a lot ! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.