0

I have a long string (e.g. AAAABBBBCCCC) and I eventually want to find all overlapping occurrences for each member of a list of different substrings (e.g. ['AAA', 'AAB', 'ABB', 'BBB']).

I found a very helpful suggestion on a previous StackOverflow posting - string count with overlapping occurrences However, using this I can't seem to assign the substrings in such a way that re.findall() can recognize them. It's probably something stupid, but I just can't seem to figure it out. It seems like the ? is doing something different than usual...

>>> string = 'AAAABBBBCCCC'
>>> len(re.findall('(?=AAA)', string))
2
>>> substring = 'AAA'
>>> len(re.findall('(?=substring)', string))
0
>>> substring = "'(?=AAA)'"
>>> len(re.findall(substring, string))
0
>>> #This works, but is not overlapping:
>>> substring = 'AAA'
>>> len(re.findall(substring, string))
1

I would appreciate any suggestions! Thanks!

Community
  • 1
  • 1
Andreanna
  • 245
  • 1
  • 5
  • 13

2 Answers2

2

If I understood you correctly, you want to assign a variable and use it in the findall function?

>>> substring = '(?=AAA)' #or "(?=AAA)"
>>> len(re.findall(substring, string))
>>> 2
  • Yeah, that's it. Duh! I guess I didn't need the second quotes because it was already considered a string : ) Thanks! – Andreanna Nov 06 '13 at 17:25
0

See if this helps you with the rest, your 5th line is string substring not variable sub string.

import re
string = 'AAAABBBBCCCC'
len(re.findall('(?=AAA)', string))
2
substring = 'AAA'
len(re.findall('(?=' + substring + ')', string))
2
jgranger
  • 264
  • 2
  • 9