MCPcopy Create free account
hub / github.com/ActiveState/code / pattern

Method pattern

recipes/Python/56036_Pure_Python_strptime/recipe-56036.py:289–309  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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."""

Callers 2

__init__Method · 0.95
compileMethod · 0.95

Calls 3

subMethod · 0.45
findMethod · 0.45
indexMethod · 0.45

Tested by

no test coverage detected