3

I have a string, s="abaaaababbb".

I am using findall method and I want to know all the occurrences of (ab)+. The code that I am using is:

import re
s = "abaaaababbb"
x = re.findall("[ab]+",s)
print(x)

Output: ['abaaaababbb']

Instead I wanted output like: ['ab' , 'abab']

How to write the correct regular expression for the same?

Hamed Ghasempour
  • 455
  • 3
  • 12
Som Shekhar Mukherjee
  • 3,348
  • 1
  • 3
  • 19

1 Answers1

2

The regex you mentioned in your question ((ab)+) is almost correct.

You just need to make the capturing group a non-capturing one:

(?:ab)+

This is because findall will return all the groups (as opposed to all the matches) if you have any capturing groups in the regex.

Sweeper
  • 145,870
  • 17
  • 129
  • 225