Marco
Marco

Reputation: 1349

Match exactly 11 occurences of the same digit in a group

I need to match exactly 11 occurences of the same digit in a group, like:

But not:

What I've tried so far can get 11 occurences of digits:

/([0-9]){11}/g
/\d{11}/g

But it will match any 11 digits.

I've managed to do this:

/(0{11}|1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11})/g

Is there any other easier way to do it?

Upvotes: 0

Views: 55

Answers (2)

Explosion Pills
Explosion Pills

Reputation: 191729

/(\d)\1{10}/

This matches the first digit and uses a reference to that digit \1 to match it ten more times. Note that this will also match if the digit repeats 12 or more times, and if other digits start the string, but this seems to be desired.

Upvotes: 3

Martin Milichovsky
Martin Milichovsky

Reputation: 730

You should use backreference: ((\d)\2{10})

The \2 matches "the same thing as was the 2nd caputing group (parentheses)".

https://regex101.com/r/QESWrJ/1

Upvotes: 3

Related Questions