Sai Krishna Palagummi Sai Krishna Palagummi - 23 days ago 7
C# Question

What regex can match sequences of the same character in a given set?

I need a regular expression that matches a sequence of characters in a given set,say {a,b,c}.

For instance aaaaaa or bb or ccc etc.

I tried using

but it will also allow other characters which doesn't belong to the set.

Is there any possible solution for this?

Answer Source

The . matches any character but a newline by default and \w matches "word" characters, that is, letters, digits and underscore symbols.

To match only specific symbol set/range(s), use a character class. For {a,b,c}, it will be [abc]:


See the regex demo


  • ([abc]) - capturing group 1 matching an a, b or c
  • \1+ - one or more occurrences (due to + quantifier) of the same character captured in Group 1.