What's a Regex pattern for 3 consecutive digits increasing or decreasing

后端 未结 3 1801
情话喂你
情话喂你 2020-12-19 12:59

I need a regex pattern to match a sequence of 3 consecutive digits in a string that are consecutively increasing or decreasing.

For example:

These strings sh

相关标签:
3条回答
  • 2020-12-19 13:36

    There is no other way than listing them:

    (012|123|234|345|456|567|678|789|987|876|765|654|543|432|321|210)
    
    0 讨论(0)
  • 2020-12-19 13:58

    This can be done simply, but only with defining the valid sets of consecutive digits

    (?:012|210|123|321|234|432|345|543|456|654|567|765|678|876|789|987)

    0 讨论(0)
  • 2020-12-19 14:00

    This is going to be a very complicated regex. On a project where I had to do something very similar, I ended up matching digit groups and then passing off the actual validation of the digits to a delegate (I was doing this in C++ code; have done something similar on another project in Java in the same way).

    If at all possible, this is what I'd recommend doing here. A regex which could do this all by itself would be very difficult to read or maintain.

    0 讨论(0)
提交回复
热议问题