Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I match the numbers sequence rising?

I have a string contains just numbers. Something like this:

var numb = "5136789431235";

And I'm trying to match ascending numbers which are two or more digits. In string above I want this output:

var numb = "5136789431235";
//             ^^^^  ^^^

Actually I can match a number which has two or more digits: /[0-9]{2,}/g, But I don't know how can I detect being ascending?

like image 456
Shafizadeh Avatar asked Apr 18 '16 10:04

Shafizadeh


2 Answers

To match consecutive numbers like 123:

(?:(?=01|12|23|34|45|56|67|78|89)\d)+\d

RegEx Demo


To match nonconsecutive numbers like 137:

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

RegEx Demo


Here is an example:

var numb = "5136789431235";

/* The output of consecutive version:    6789,123
   The output of nonconsecutive version: 136789,1234
*/
like image 186
Tim007 Avatar answered Oct 23 '22 15:10

Tim007


You could do this by simply testing for

01|12|23|34|45|56|67|78|89

Regards

like image 2
SamWhan Avatar answered Oct 23 '22 16:10

SamWhan