So I have a little problem,
I want to count how many times a string : "aa"
is in my longer string "aaatattgg"
its looks like a dna sequence.
Here for exemple I expect 2 (overlap is allow)
There is the .count
method but overlap is not allowed
PS: excuse my english , I'm french
Asked
Active
Viewed 109 times
0
-
you can start from the first character.. and start comparing it. like in basic string matching – Milind Dumbare Mar 07 '15 at 13:45
1 Answers
1
Through re module. Put your regex inside positive lookarounds in-order to do overlapping match.
>>> import re
>>> s = "aaatattgg"
>>> re.findall(r'(?=(aa))', s)
['aa', 'aa']
>>> len(re.findall(r'(?=(aa))', s))
2

Avinash Raj
- 172,303
- 28
- 230
- 274