Zane
Zane

Reputation: 15

Regex: 3 digits in increasing order and same number

I want to check if the last 3 digits of the sequence match the 3 preceding numbers and the value of the elements in those 3 numbers must increase using Regex.

example:

i tried use that expression:

(\d)(?!\1)(\d)(?!\1)(?!\2)(\d)\1\2\3

but i only get last 3 digits of the sequence match the 3 preceding, without regard to increase

Upvotes: 0

Views: 594

Answers (1)

JvdV
JvdV

Reputation: 75840

I guess you can use:

(012|123|234|345|456|567|678|789)\1$

See the online demo.

  • (012|123|234|345|456|567|678|789) - Match any of these increasing sequences.
  • \1 - A backreference to match the same sequence of this 1st capture group.
  • $ - End string anchor.

Your match will be the 1st capture group.

Note: If you want to also account for alternations like 013|014....245|246 etc, you'd have to include those amongst those alternations. That would come down to 120 alternatives... not very pretty:

(012|013|014|015|016|017|018|019|023|024|025|026|027|028|029|034|035|036|037|038|039|045|046|047|048|049|056|057|058|059|067|068|069|078|079|089|123|124|125|126|127|128|129|134|135|136|137|138|139|145|146|147|148|149|156|157|158|159|167|168|169|178|179|189|234|235|236|237|238|239|245|246|247|248|249|256|257|258|259|267|268|269|278|279|289|345|346|347|348|349|356|357|358|359|367|368|369|378|379|389|456|457|458|459|467|468|469|478|479|489|567|568|569|578|579|589|678|679|689|789)\1$

Maybe your app has a way to post-process the capture group as a better alternative.

Upvotes: 1

Related Questions