Search code examples
pythonpython-re

How can I find all matches in any position in the string using regular expression (re.finditer) in Python?


I am using python regular expression (regex) to find all the matches in any string position (say "GgGAT", frame 1 is Gg.. and frame2 is gG..). I need to used re.finditer method. My code below gives me only "Gg" while I need both "Gg" and "gG";

import re

data="ACGTGgGTT" 
for match in re.finditer(r'GG|gg|Gg|gG', data):
   print (match)

Solution

  • You can use regex lookahead using ?=... synthax:

    re.finditer(r"(?=(GG|gg|Gg|gG))", data)