tags:

views:

107

answers:

2

hi,
Thanks in advance.I want to find all the substring that occurs between K and N,eventhough K and N occurs in between any number of times. for example
a='KANNKAAN'

OUTPUT;
[KANNKAAN, KANN , KAN ,KAAN]

+1  A: 
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")

Is that what You need?

Reef
yes thanks.useful for my sequence analysis
+1  A: 

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']
dF