forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathre_tools.py
More file actions
30 lines (22 loc) · 677 Bytes
/
re_tools.py
File metadata and controls
30 lines (22 loc) · 677 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Copyright 2009-2010 Ram Rachum.
# This program is distributed under the MIT license.
'''Defines miscellaneous tools.'''
import re
def searchall(pattern, string, flags=0):
'''
Return all the substrings of `string` that match `pattern`.
Note: Currently returns only non-overlapping matches.
'''
if isinstance(pattern, str):
pattern = re.compile(pattern, flags=flags)
matches = []
start = 0
end = len(string)
while True:
match = pattern.search(string, start, end)
if match:
matches.append(match)
start = match.end()
else:
break
return matches