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)
| 287 | return '%s)' % regex |
| 288 | |
| 289 | def pattern(self, format): |
| 290 | """Return regex pattern for the format string. |
| 291 | |
| 292 | Need to make sure that any characters that might be interpreted as |
| 293 | regex syntax are escaped. |
| 294 | |
| 295 | """ |
| 296 | processed_format = '' |
| 297 | # The sub() call escapes all characters that might be misconstrued |
| 298 | # as regex syntax. |
| 299 | regex_chars = re_compile(r"([\\.^$*+?{}\[\]|])") |
| 300 | format = regex_chars.sub(r"\\\1", format) |
| 301 | whitespace_replacement = re_compile('\s+') |
| 302 | format = whitespace_replacement.sub('\s*', format) |
| 303 | while format.find('%') != -1: |
| 304 | directive_index = format.index('%')+1 |
| 305 | processed_format = "%s%s%s" % (processed_format, |
| 306 | format[:directive_index-1], |
| 307 | self[format[directive_index]]) |
| 308 | format = format[directive_index+1:] |
| 309 | return "%s%s" % (processed_format, format) |
| 310 | |
| 311 | def compile(self, format): |
| 312 | """Return a compiled re object for the format string.""" |