python 3.x – Get counts of repeated letters using Regex

I would like a regex that returns all of the A’s and all of the B’s in a string like ‘AABCTA’ with similar characters grouped together in the result i.e. [AAA, BB]. Note that there may be thousands of repeated letters.

This works for just the A’s:

re.findall(r'[A]','AABCTBA')
['A', 'A', 'A']

but this:

re.findall(r'[A B]','AABCTBA')

returns:

['A', 'A', 'B', 'A']

I want ['A' 'A' 'A', 'B'].

What I ultimately want is a count of the letters so if there is a different way to get the letter count using regex I’d love to see that.

Source link