Find substring
2 answers
import re
def occurences(ch_searched, str_input):
return [i.start() for i in re.finditer(ch_searched, str_input)]
def betweeners(str_input, ch_from, ch_to):
starts = occurences(ch_from, str_input)
ends = occurences(ch_to, str_input)
result = []
for start in starts:
for end in ends:
if start<end:
result.append( str_input[start:end+1] )
return result
print betweeners('KANNKAAN', "K", "N")
This is what you need?
+2
a source to share
Another way:
def findbetween(text, begin, end):
for match in re.findall(begin + '.*' +end, text):
yield match
for m in findbetween(match[1:], begin, end):
yield m
for m in findbetween(match[:-1], begin, end):
yield m
>>> list(findbetween('KANNKAAN', 'K', 'N'))
['KANNKAAN', 'KAAN', 'KANN', 'KAN']
+1
a source to share