-1

I'm trying to use .NET regular expressions to detect cases where a string has 5 or more characters and the only character in the string is the same. So these would be seen as matches:

  • 0000000000000
  • ZZZZZZZZZ
  • AAAAAA

But these wouldn't:

  • 000000A000000
  • ZZZZZZ ZZZ Z
  • AA

Is there a regular expression that could detect this pattern?

user2148983
  • 138
  • 2
  • 13

1 Answers1

3

Wiktor was close, but his will only match exactly 5 characters. Sounds like you want 5 or more. So what I'd use is

^(.)\1{4,}$

The (.)\1 will capture exactly one character. The {4,} specifies that the one character is then repeated 4 or more times. Wrapping it all in ^ and $ means the string starts immediately before that and ends immediately after, so nothing else is allowed to come before or after it.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Arin
  • 1,373
  • 2
  • 10
  • 23