khan rooha
khan rooha

Reputation: 11

regular expression for string containing repetition of same alphabet

I am new to regular expressions. I am looking for String which have only repeated characters like (aaa or bbb) from A to Z.

I am using @"\b[a-z]{1}(.){1,}\b" which is partially working. Not completely. I would appreciate if anyone help me in this regard. Thanks.

Upvotes: 0

Views: 54

Answers (1)

The fourth bird
The fourth bird

Reputation: 163287

You could capture the first character in a capturing group (group 1).

Then make sure the rest of the string contains the same character as the character captured in group 1 by using a backreference to group 1 like \1:

\b([a-zA-Z])\1+\b

Explanation

  • \b Word boundary
  • ([a-zA-Z]) Match an upper or lowercase character in the first capturing group
  • \1+ Repeat the first caturing group 1 or more times
  • \b Word boundary

Upvotes: 2

Related Questions