1

In Regex, I am given a string "abbabbabba" and I have to identify all the sub-strings having the pattern "abba" and when I try it this way:

import re
a=re.findall(r'abba','abbabbabba')
print(a)

I get my out put to be

['abba', 'abba']

Looking at the given string, there are 3 sub-strings of 'abba' but only two have been printed. This clearly means that the second sub-string which has its 'a' already consumed in 1st sub-string isn't allowing the function to identify the 2nd sub-string and hence it is directly jumping to the 3rd sub-string and hence only 2 'abba' sub-strings are printed. How to make sure that the input isn't consumed in this way and we get the output accordingly as:

['abba', 'abba', 'abba']  
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78

0 Answers0