RegExp match repeated characters

Andrew picture Andrew · Jun 10, 2011 · Viewed 25.2k times · Source

For example I have string:

 aacbbbqq

As the result I want to have following matches:

 (aa, c, bbb, qq)  

I know that I can write something like this:

 ([a]+)|([b]+)|([c]+)|...  

But I think i's ugly and looking for better solution. I'm looking for regular expression solution, not self-written finite-state machines.

Answer

Qtax picture Qtax · Jun 10, 2011

You can match that with: (\w)\1*