Return regex pattern for the format string. Need to make sure that any characters that might be interpreted as regex syntax are escaped.
(self, format)
| 236 | return '%s)' % regex |
| 237 | |
| 238 | def pattern(self, format): |
| 239 | """Return regex pattern for the format string. |
| 240 | |
| 241 | Need to make sure that any characters that might be interpreted as |
| 242 | regex syntax are escaped. |
| 243 | |
| 244 | """ |
| 245 | processed_format = '' |
| 246 | # The sub() call escapes all characters that might be misconstrued |
| 247 | # as regex syntax. Cannot use re.escape since we have to deal with |
| 248 | # format directives (%m, etc.). |
| 249 | regex_chars = re_compile(r"([\\.^$*+?\(\){}\[\]|])") |
| 250 | format = regex_chars.sub(r"\\\1", format) |
| 251 | whitespace_replacement = re_compile(r'\s+') |
| 252 | format = whitespace_replacement.sub(r'\\s+', format) |
| 253 | while '%' in format: |
| 254 | directive_index = format.index('%')+1 |
| 255 | processed_format = "%s%s%s" % (processed_format, |
| 256 | format[:directive_index-1], |
| 257 | self[format[directive_index]]) |
| 258 | format = format[directive_index+1:] |
| 259 | return "%s%s" % (processed_format, format) |
| 260 | |
| 261 | def compile(self, format): |
| 262 | """Return a compiled re object for the format string.""" |