Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a number string is in running sequence

Tags:

c#

.net

regex

logic

Is there a regex (or any other way) to check if a numbers in a string is in running sequence?

E.g.,

"123456" will return true
"456789" will return true
"345678" will return true
"123467" will return false
"901234" will return false
like image 665
Null Reference Avatar asked May 27 '14 03:05

Null Reference


2 Answers

If all your sequences are composed of single-digit numbers, then you can solve this by observing that all correct sequences must be substrings of the longest such sequence, i.e. of "0123456789". So the check can be done like this:

bool res = "0123456789".Contains(str);

Demo on ideone.

like image 171
Sergey Kalinichenko Avatar answered Nov 08 '22 07:11

Sergey Kalinichenko


How about this:

text.Skip(1).Zip(text, (c1, c0) => new { c1, c0 }).All(c => c.c1 - c.c0 == 1)
like image 27
Enigmativity Avatar answered Nov 08 '22 08:11

Enigmativity