fork download
  1. import re
  2.  
  3. def findall_overlapped(r, s):
  4. res = [] # Resulting list
  5. reg = r'^{}$'.format(r) # Regex must match full string
  6. for q in range(len(s)): # Iterate over all chars in a string
  7. for w in range(q,len(s)): # Iterate over the rest of the chars to the right
  8. cur = s[q:w+1] # Currently tested slice
  9. m = re.match(reg, cur) # If there is a full slice match
  10. if m:
  11. res.append(m.group(1)) # Append Group 1 value to the resulting list
  12. return res
  13.  
  14. rx = r'foo", "(.*?)", "bar"'
  15. text = '"lorem ipsum", "foo", "baz", "bar", "lorem ipsum", "bar", "ipsum", "foo", "baz", "bar"'
  16. for s in findall_overlapped(rx, text):
  17. print(s)
Success #stdin #stdout 0.02s 9412KB
stdin
Standard input is empty
stdout
baz
baz", "bar", "lorem ipsum
baz", "bar", "lorem ipsum", "bar", "ipsum", "foo", "baz
baz