Using regex to match numbers which have 5 increasing consecutive digits somewhere in them

  • A+

First off, this has sort of been asked before. However I haven't been able to modify this to fit my requirement.

In short: I want a regex that matches an expression if and only if it only contains digits, and there are 5 (or more) increasing consecutive digits somewhere in the expression.

I understand the logic of


however, this limits the expression to 5 digits. I want there to be able to be digits before and after the expression. So 1111345671111 should match, while 11111 shouldn't.

I thought this might work:


which I interpret as:

  • ^$: The entire expression must only contain what's between these 2 symbols

  • [0-9]*: Any digits between 0-9, 0 or more times followed by:

  • (?=/d{5}0*1*2*3*4*5*6*7*8*9*): A part where at least 5 increasing digits are found followed by:

  • [0-9]*: Any digits between 0-9, 0 or more times.

However this regex is incorrect, as for example 11111 matches. How can I solve this problem using a regex? So examples of expressions to match:

  • 00001459000
  • 12345

This shouldn't match:

  • abc12345
  • 9871234444


While this problem can be solved using pure regular expressions (the set of strictly ascending five-digit strings is finite, so you could just enumerate all of them), it's not a good fit for regexes.

That said, here's how I'd do it if I had to:


Core idea: 0?1?2?3?4?5?6?7?8?9? matches an ascending numeric substring, but it doesn't restrict its length. Every single part is optional, so it can match anything from "" (empty string) to the full "0123456789".

We can force it to match exactly 5 characters by combining a look-ahead of five digits and an arbitrary suffix (which we capture) and a backreference /1 (which must exactly the suffix matched by the look-ahead, ensuring we've now walked ahead 5 characters in the string).

Live demo:

(By the way, your explanation of (?=/d{5}0*1*2*3*4*5*6*7*8*9*) is incorrect: It looks ahead to match exactly 5 digits, followed by 0 or more occurrences of 0, followed by 0 or more occurrences of 1, etc.)


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: