This pattern will match if the user enters the same number:
^(\d)\1*$
\1 matches the first capture group, so the pattern matches whether this first digit is repeated for the entire line.
The second problem (consecutive numbers) is somewhat more complicated.
^(?:^(?:^(?:^(?:^0?1)?2)?3)4?)?5(?:$|6(?:$|7(?:$|8(?:$|90?))))$| ^(0?1)?2(?:$|3(?:$|4))|^(6?7)?8(?:$|90?)$
- one implementation involving three or more digits. But since the number of combinations is small, it is also possible to enumerate (4 + digits):
^(?:0?123(45?)?|1?23456?|2?34567?|3?45678?|4?56789?|(5?6)?7890?| (0?1)?2345678?(90$)?|1?23456789?|2?345678(90?)?)$
All this suggests that regular expressions do not always work well for this type of problem. The Java method for checking this sequence may be cleaner.
source share