|
1 | 1 | import re |
2 | 2 | import operator |
3 | | -from six.moves import reduce |
4 | 3 | import six |
5 | 4 |
|
6 | 5 | REX_CACHE = {} |
@@ -40,15 +39,20 @@ class Rex(object): |
40 | 39 | 'x': re.VERBOSE, |
41 | 40 | } |
42 | 41 |
|
43 | | - def __init__(self, action, pattern, replacement='', flags=0): |
| 42 | + EXTRA_FLAGS = 'g' |
| 43 | + |
| 44 | + def __init__(self, action, pattern, replacement='', flags=0, extra_flags=''): |
44 | 45 | self.action = action |
45 | 46 | self.pattern = pattern |
46 | 47 | self.flags = flags |
| 48 | + self.extra_flags = extra_flags |
47 | 49 | self.replacement = replacement |
48 | 50 | self.re = re.compile(self.pattern, self.flags) |
49 | 51 |
|
50 | 52 | def __process(self, text): |
51 | 53 | if self.action == 'm': |
| 54 | + if 'g' in self.extra_flags: |
| 55 | + return self.re.findall(text) |
52 | 56 | result = RexMatch() |
53 | 57 | match = self.re.search(text) |
54 | 58 | if match is not None: |
@@ -95,12 +99,17 @@ def rex(expression, text=None, cache=True): |
95 | 99 | replacement = pattern[index + 1:] |
96 | 100 | pattern = pattern[:index] |
97 | 101 |
|
98 | | - try: |
99 | | - re_flags = [Rex.FLAGS[f] for f in expression[end + 1:]] |
100 | | - except KeyError: |
101 | | - raise ValueError('Bad flags') |
| 102 | + flags = 0 |
| 103 | + extra_flags = '' |
| 104 | + for f in expression[end + 1:]: |
| 105 | + if f in Rex.FLAGS: |
| 106 | + flags |= Rex.FLAGS[f] |
| 107 | + elif f in Rex.EXTRA_FLAGS: |
| 108 | + extra_flags += f |
| 109 | + else: |
| 110 | + raise ValueError('Bad flags') |
102 | 111 |
|
103 | | - rex_obj = Rex(action, pattern, replacement, reduce(operator.or_, re_flags, 0)) |
| 112 | + rex_obj = Rex(action, pattern, replacement, flags, extra_flags) |
104 | 113 | if cache: |
105 | 114 | REX_CACHE[expression] = rex_obj |
106 | 115 |
|
|
0 commit comments