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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












16















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



^(?=d5$)1*2*3*4*5*6*7*8*9*0*$


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:



^[0-9]*(?=d50*1*2*3*4*5*6*7*8*9*)[0-9]*$


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:


  • (?=d50*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









share|improve this question



















  • 7





    Regex isn't really suitable for validating strings' semantics. Leave that for your code to do.

    – Sweeper
    Jan 26 at 20:24











  • It might be easier if you split it up into two expressions: string must contain only digits ^[0-9]+$ AND string must contain 5 increasing digits (but I'm not sure what this regex would be).

    – wjandrea
    Jan 26 at 20:43















16















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



^(?=d5$)1*2*3*4*5*6*7*8*9*0*$


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:



^[0-9]*(?=d50*1*2*3*4*5*6*7*8*9*)[0-9]*$


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:


  • (?=d50*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









share|improve this question



















  • 7





    Regex isn't really suitable for validating strings' semantics. Leave that for your code to do.

    – Sweeper
    Jan 26 at 20:24











  • It might be easier if you split it up into two expressions: string must contain only digits ^[0-9]+$ AND string must contain 5 increasing digits (but I'm not sure what this regex would be).

    – wjandrea
    Jan 26 at 20:43













16












16








16


3






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



^(?=d5$)1*2*3*4*5*6*7*8*9*0*$


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:



^[0-9]*(?=d50*1*2*3*4*5*6*7*8*9*)[0-9]*$


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:


  • (?=d50*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









share|improve this question
















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



^(?=d5$)1*2*3*4*5*6*7*8*9*0*$


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:



^[0-9]*(?=d50*1*2*3*4*5*6*7*8*9*)[0-9]*$


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:


  • (?=d50*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






regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 26 at 21:43









Wai Ha Lee

5,857123964




5,857123964










asked Jan 26 at 20:09









oscarloooscarloo

813




813







  • 7





    Regex isn't really suitable for validating strings' semantics. Leave that for your code to do.

    – Sweeper
    Jan 26 at 20:24











  • It might be easier if you split it up into two expressions: string must contain only digits ^[0-9]+$ AND string must contain 5 increasing digits (but I'm not sure what this regex would be).

    – wjandrea
    Jan 26 at 20:43












  • 7





    Regex isn't really suitable for validating strings' semantics. Leave that for your code to do.

    – Sweeper
    Jan 26 at 20:24











  • It might be easier if you split it up into two expressions: string must contain only digits ^[0-9]+$ AND string must contain 5 increasing digits (but I'm not sure what this regex would be).

    – wjandrea
    Jan 26 at 20:43







7




7





Regex isn't really suitable for validating strings' semantics. Leave that for your code to do.

– Sweeper
Jan 26 at 20:24





Regex isn't really suitable for validating strings' semantics. Leave that for your code to do.

– Sweeper
Jan 26 at 20:24













It might be easier if you split it up into two expressions: string must contain only digits ^[0-9]+$ AND string must contain 5 increasing digits (but I'm not sure what this regex would be).

– wjandrea
Jan 26 at 20:43





It might be easier if you split it up into two expressions: string must contain only digits ^[0-9]+$ AND string must contain 5 increasing digits (but I'm not sure what this regex would be).

– wjandrea
Jan 26 at 20:43












2 Answers
2






active

oldest

votes


















23














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:



^d*(?=d5(d*)$)0?1?2?3?4?5?6?7?8?9?1$


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: https://regex101.com/r/03rJET/3



(By the way, your explanation of (?=d50*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.)






share|improve this answer

























  • I am an upvoter and find it useful to know that it's possible to capture things inside a positive lookahead.

    – Tim Biegeleisen
    Jan 26 at 21:13











  • @TimBiegeleisen It's neat, isn't it? I think it feels a bit like getting a vision of the future, but being able to take and keep something from it.

    – melpomene
    Jan 26 at 21:15











  • I don't know if it's by design the OP includes ^$ anchors or what, but without them (?=d5(d*))0?1?2?3?4?5?6?7?8?9?1 has no chance to match as a substring.

    – sln
    Jan 26 at 22:49


















3














Because the starting position of the increasing digits isn't known in advance, and the consecutive increasing digits don't end at the end of the string, the linked answer's concise pattern won't work here. I don't think this is possible without being repetitive; alternate between all possibilities of increasing digits. A 0 must be followed by [1-9]. (0(?=[1-9])) A 1 must be followed by [2-9]. A 2 must be followed by [3-9], and so on. Alternate between these possibilities in a group, and repeat that group four times, and then match any digit after that (the lookahead in the last repeated digit in the previous group will ensure that this 5th digit is in sequence as well).



First lookahead for digits followed by the end of the string, then match the alternations described above, followed by one or more digits:



^(?=d+$)d*?(?:0(?=[1-9])|1(?=[2-9])|2(?=[3-9])|3(?=[4-9])|4(?=[5-9])|5(?=[6-9])|6(?=[7-9])|7(?=[89])|8(?=9))4d+


Separated out for better readability:



^(?=d+$)d*?
(?:
0(?=[1-9])|
1(?=[2-9])|
2(?=[3-9])|
3(?=[4-9])|
4(?=[5-9])|
5(?=[6-9])|
6(?=[7-9])|
7(?=[89])|
8(?=9)
)4
d+


The lazy quantifier in the first line there d*? isn't necessary, but it makes the pattern a bit more efficient (otherwise it initially greedily matches the whole string, requiring lots of failing alternations and backtracking until at least 5 characters before the end of the string)



https://regex101.com/r/03rJET/2



It's ugly, but it works.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54382276%2fusing-regex-to-match-numbers-which-have-5-increasing-consecutive-digits-somewher%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    23














    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:



    ^d*(?=d5(d*)$)0?1?2?3?4?5?6?7?8?9?1$


    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: https://regex101.com/r/03rJET/3



    (By the way, your explanation of (?=d50*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.)






    share|improve this answer

























    • I am an upvoter and find it useful to know that it's possible to capture things inside a positive lookahead.

      – Tim Biegeleisen
      Jan 26 at 21:13











    • @TimBiegeleisen It's neat, isn't it? I think it feels a bit like getting a vision of the future, but being able to take and keep something from it.

      – melpomene
      Jan 26 at 21:15











    • I don't know if it's by design the OP includes ^$ anchors or what, but without them (?=d5(d*))0?1?2?3?4?5?6?7?8?9?1 has no chance to match as a substring.

      – sln
      Jan 26 at 22:49















    23














    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:



    ^d*(?=d5(d*)$)0?1?2?3?4?5?6?7?8?9?1$


    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: https://regex101.com/r/03rJET/3



    (By the way, your explanation of (?=d50*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.)






    share|improve this answer

























    • I am an upvoter and find it useful to know that it's possible to capture things inside a positive lookahead.

      – Tim Biegeleisen
      Jan 26 at 21:13











    • @TimBiegeleisen It's neat, isn't it? I think it feels a bit like getting a vision of the future, but being able to take and keep something from it.

      – melpomene
      Jan 26 at 21:15











    • I don't know if it's by design the OP includes ^$ anchors or what, but without them (?=d5(d*))0?1?2?3?4?5?6?7?8?9?1 has no chance to match as a substring.

      – sln
      Jan 26 at 22:49













    23












    23








    23







    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:



    ^d*(?=d5(d*)$)0?1?2?3?4?5?6?7?8?9?1$


    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: https://regex101.com/r/03rJET/3



    (By the way, your explanation of (?=d50*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.)






    share|improve this answer















    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:



    ^d*(?=d5(d*)$)0?1?2?3?4?5?6?7?8?9?1$


    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: https://regex101.com/r/03rJET/3



    (By the way, your explanation of (?=d50*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.)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 26 at 21:04

























    answered Jan 26 at 20:58









    melpomenemelpomene

    60.7k54793




    60.7k54793












    • I am an upvoter and find it useful to know that it's possible to capture things inside a positive lookahead.

      – Tim Biegeleisen
      Jan 26 at 21:13











    • @TimBiegeleisen It's neat, isn't it? I think it feels a bit like getting a vision of the future, but being able to take and keep something from it.

      – melpomene
      Jan 26 at 21:15











    • I don't know if it's by design the OP includes ^$ anchors or what, but without them (?=d5(d*))0?1?2?3?4?5?6?7?8?9?1 has no chance to match as a substring.

      – sln
      Jan 26 at 22:49

















    • I am an upvoter and find it useful to know that it's possible to capture things inside a positive lookahead.

      – Tim Biegeleisen
      Jan 26 at 21:13











    • @TimBiegeleisen It's neat, isn't it? I think it feels a bit like getting a vision of the future, but being able to take and keep something from it.

      – melpomene
      Jan 26 at 21:15











    • I don't know if it's by design the OP includes ^$ anchors or what, but without them (?=d5(d*))0?1?2?3?4?5?6?7?8?9?1 has no chance to match as a substring.

      – sln
      Jan 26 at 22:49
















    I am an upvoter and find it useful to know that it's possible to capture things inside a positive lookahead.

    – Tim Biegeleisen
    Jan 26 at 21:13





    I am an upvoter and find it useful to know that it's possible to capture things inside a positive lookahead.

    – Tim Biegeleisen
    Jan 26 at 21:13













    @TimBiegeleisen It's neat, isn't it? I think it feels a bit like getting a vision of the future, but being able to take and keep something from it.

    – melpomene
    Jan 26 at 21:15





    @TimBiegeleisen It's neat, isn't it? I think it feels a bit like getting a vision of the future, but being able to take and keep something from it.

    – melpomene
    Jan 26 at 21:15













    I don't know if it's by design the OP includes ^$ anchors or what, but without them (?=d5(d*))0?1?2?3?4?5?6?7?8?9?1 has no chance to match as a substring.

    – sln
    Jan 26 at 22:49





    I don't know if it's by design the OP includes ^$ anchors or what, but without them (?=d5(d*))0?1?2?3?4?5?6?7?8?9?1 has no chance to match as a substring.

    – sln
    Jan 26 at 22:49













    3














    Because the starting position of the increasing digits isn't known in advance, and the consecutive increasing digits don't end at the end of the string, the linked answer's concise pattern won't work here. I don't think this is possible without being repetitive; alternate between all possibilities of increasing digits. A 0 must be followed by [1-9]. (0(?=[1-9])) A 1 must be followed by [2-9]. A 2 must be followed by [3-9], and so on. Alternate between these possibilities in a group, and repeat that group four times, and then match any digit after that (the lookahead in the last repeated digit in the previous group will ensure that this 5th digit is in sequence as well).



    First lookahead for digits followed by the end of the string, then match the alternations described above, followed by one or more digits:



    ^(?=d+$)d*?(?:0(?=[1-9])|1(?=[2-9])|2(?=[3-9])|3(?=[4-9])|4(?=[5-9])|5(?=[6-9])|6(?=[7-9])|7(?=[89])|8(?=9))4d+


    Separated out for better readability:



    ^(?=d+$)d*?
    (?:
    0(?=[1-9])|
    1(?=[2-9])|
    2(?=[3-9])|
    3(?=[4-9])|
    4(?=[5-9])|
    5(?=[6-9])|
    6(?=[7-9])|
    7(?=[89])|
    8(?=9)
    )4
    d+


    The lazy quantifier in the first line there d*? isn't necessary, but it makes the pattern a bit more efficient (otherwise it initially greedily matches the whole string, requiring lots of failing alternations and backtracking until at least 5 characters before the end of the string)



    https://regex101.com/r/03rJET/2



    It's ugly, but it works.






    share|improve this answer





























      3














      Because the starting position of the increasing digits isn't known in advance, and the consecutive increasing digits don't end at the end of the string, the linked answer's concise pattern won't work here. I don't think this is possible without being repetitive; alternate between all possibilities of increasing digits. A 0 must be followed by [1-9]. (0(?=[1-9])) A 1 must be followed by [2-9]. A 2 must be followed by [3-9], and so on. Alternate between these possibilities in a group, and repeat that group four times, and then match any digit after that (the lookahead in the last repeated digit in the previous group will ensure that this 5th digit is in sequence as well).



      First lookahead for digits followed by the end of the string, then match the alternations described above, followed by one or more digits:



      ^(?=d+$)d*?(?:0(?=[1-9])|1(?=[2-9])|2(?=[3-9])|3(?=[4-9])|4(?=[5-9])|5(?=[6-9])|6(?=[7-9])|7(?=[89])|8(?=9))4d+


      Separated out for better readability:



      ^(?=d+$)d*?
      (?:
      0(?=[1-9])|
      1(?=[2-9])|
      2(?=[3-9])|
      3(?=[4-9])|
      4(?=[5-9])|
      5(?=[6-9])|
      6(?=[7-9])|
      7(?=[89])|
      8(?=9)
      )4
      d+


      The lazy quantifier in the first line there d*? isn't necessary, but it makes the pattern a bit more efficient (otherwise it initially greedily matches the whole string, requiring lots of failing alternations and backtracking until at least 5 characters before the end of the string)



      https://regex101.com/r/03rJET/2



      It's ugly, but it works.






      share|improve this answer



























        3












        3








        3







        Because the starting position of the increasing digits isn't known in advance, and the consecutive increasing digits don't end at the end of the string, the linked answer's concise pattern won't work here. I don't think this is possible without being repetitive; alternate between all possibilities of increasing digits. A 0 must be followed by [1-9]. (0(?=[1-9])) A 1 must be followed by [2-9]. A 2 must be followed by [3-9], and so on. Alternate between these possibilities in a group, and repeat that group four times, and then match any digit after that (the lookahead in the last repeated digit in the previous group will ensure that this 5th digit is in sequence as well).



        First lookahead for digits followed by the end of the string, then match the alternations described above, followed by one or more digits:



        ^(?=d+$)d*?(?:0(?=[1-9])|1(?=[2-9])|2(?=[3-9])|3(?=[4-9])|4(?=[5-9])|5(?=[6-9])|6(?=[7-9])|7(?=[89])|8(?=9))4d+


        Separated out for better readability:



        ^(?=d+$)d*?
        (?:
        0(?=[1-9])|
        1(?=[2-9])|
        2(?=[3-9])|
        3(?=[4-9])|
        4(?=[5-9])|
        5(?=[6-9])|
        6(?=[7-9])|
        7(?=[89])|
        8(?=9)
        )4
        d+


        The lazy quantifier in the first line there d*? isn't necessary, but it makes the pattern a bit more efficient (otherwise it initially greedily matches the whole string, requiring lots of failing alternations and backtracking until at least 5 characters before the end of the string)



        https://regex101.com/r/03rJET/2



        It's ugly, but it works.






        share|improve this answer















        Because the starting position of the increasing digits isn't known in advance, and the consecutive increasing digits don't end at the end of the string, the linked answer's concise pattern won't work here. I don't think this is possible without being repetitive; alternate between all possibilities of increasing digits. A 0 must be followed by [1-9]. (0(?=[1-9])) A 1 must be followed by [2-9]. A 2 must be followed by [3-9], and so on. Alternate between these possibilities in a group, and repeat that group four times, and then match any digit after that (the lookahead in the last repeated digit in the previous group will ensure that this 5th digit is in sequence as well).



        First lookahead for digits followed by the end of the string, then match the alternations described above, followed by one or more digits:



        ^(?=d+$)d*?(?:0(?=[1-9])|1(?=[2-9])|2(?=[3-9])|3(?=[4-9])|4(?=[5-9])|5(?=[6-9])|6(?=[7-9])|7(?=[89])|8(?=9))4d+


        Separated out for better readability:



        ^(?=d+$)d*?
        (?:
        0(?=[1-9])|
        1(?=[2-9])|
        2(?=[3-9])|
        3(?=[4-9])|
        4(?=[5-9])|
        5(?=[6-9])|
        6(?=[7-9])|
        7(?=[89])|
        8(?=9)
        )4
        d+


        The lazy quantifier in the first line there d*? isn't necessary, but it makes the pattern a bit more efficient (otherwise it initially greedily matches the whole string, requiring lots of failing alternations and backtracking until at least 5 characters before the end of the string)



        https://regex101.com/r/03rJET/2



        It's ugly, but it works.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 26 at 20:58

























        answered Jan 26 at 20:47









        CertainPerformanceCertainPerformance

        86.4k154673




        86.4k154673



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54382276%2fusing-regex-to-match-numbers-which-have-5-increasing-consecutive-digits-somewher%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?