| 136 | |
| 137 | |
| 138 | class MatchObject(object): |
| 139 | |
| 140 | offset_warning = u"This returns byte offsets; use with {}.{}::string" |
| 141 | |
| 142 | def __bool__(self): |
| 143 | return True |
| 144 | |
| 145 | def __nonzero__(self): |
| 146 | return self.__bool__() |
| 147 | |
| 148 | def __init__(self, pos, endpos, re, string, captures): |
| 149 | self.pos = pos |
| 150 | self.endpos = pos |
| 151 | self.re = re |
| 152 | self.string = string |
| 153 | self._captures = captures |
| 154 | |
| 155 | def _warn(self, mname): |
| 156 | warnings.warn( |
| 157 | self.offset_warning.format( |
| 158 | self.__class__.__module__, |
| 159 | self.__class__.__name__, |
| 160 | mname), |
| 161 | UnicodeWarning, |
| 162 | stacklevel=2) |
| 163 | |
| 164 | @property |
| 165 | def lastindex(self): |
| 166 | groups = [ |
| 167 | gindex |
| 168 | for gindex, gname in enumerate(self.re.capture_names()) |
| 169 | if self.captures[gindex] is not None |
| 170 | ] |
| 171 | if groups: |
| 172 | return groups[-1] |
| 173 | |
| 174 | @property |
| 175 | def lastgroup(self, decode=True): |
| 176 | groups = [ |
| 177 | gname |
| 178 | for gindex, gname in enumerate(self.re.capture_names()) |
| 179 | if gname is not None and self.captures[gindex] is not None |
| 180 | ] |
| 181 | if groups: |
| 182 | if decode: |
| 183 | return groups[-1].decode('utf8') |
| 184 | else: |
| 185 | return groups[-1] |
| 186 | |
| 187 | @property |
| 188 | def captures(self): |
| 189 | if isinstance(self._captures, Integral): |
| 190 | all_captures = list(self.re._rure.captures_iter(self.string, |
| 191 | self.pos)) |
| 192 | self._captures = all_captures[self._captures] |
| 193 | return self._captures |
| 194 | |
| 195 | def expand(self, template): |