Skip to content

Instantly share code, notes, and snippets.

@SCOTT-HAMILTON
Last active June 8, 2019 16:08
Show Gist options
  • Save SCOTT-HAMILTON/773516f210812df534b2e8e1aa28f061 to your computer and use it in GitHub Desktop.
Save SCOTT-HAMILTON/773516f210812df534b2e8e1aa28f061 to your computer and use it in GitHub Desktop.
A function that find all occurencies of a regex in a string (returns a list of indexes).
def regex_findall(regex, content, flags=0):
res = re.findall(regex, content, flags)
indexes = []
adder = 0
for str in res:
s = len(content)
str = ''.join(str)
index = content.find(str)
if index != -1:
print(index)
content = content[(index+len(str)):]
indexes.append(index+adder)
adder = s-len(content)
return indexes
# actually the same as doing :
# [m.start(0) for m in re.finditer(pattern, content)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment