Last active
June 8, 2019 16:08
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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