Overlapping regex matches

后端 未结 3 2053
抹茶落季
抹茶落季 2020-12-21 19:20

I\'m trying to create the following regular expression: return a string between AUG and (UAG or UGA or UAA) from a follow

3条回答
  •  眼角桃花
    2020-12-21 19:51

    Unfortunately the re module does not offer support for overlapping matches at the moment, but you can easily break the solution down like so:

    'AGCCAUGUAGCUAACUCAGGUUACAUGGGGAUGACCCCGCGACUUGGAUUAGAGUCUCUUUUGGAAUAAGCCUGAAUGAUCCGAGUAGCAUCUCAG'
    matches = []
    
    for m in re.finditer('AUG', str):
        for n in re.finditer('(UAG)|(UGA)|(UAA)', str[m.start():]):
            matches.append(str[m.start()+3:m.start()+n.end()-3]
    
    print matches
    

提交回复
热议问题