pytoolbox.regex module¶
- pytoolbox.regex.embed_in_regex(string, regex_parts, index, as_string=True)[source]¶
Example usage
>>> embed_in_regex('L', ['[a-z]', '[a-z]'], 0) (0, 'L[a-z]') >>> embed_in_regex('L', ['[a-z]', '[a-z]'], 1) (1, '[a-z]L') >>> embed_in_regex('L', ['[a-z]', '[a-z]'], 1, as_string=False) (1, ['[a-z]', 'L'])
- pytoolbox.regex.findall_partial(string, regex_parts)[source]¶
Example usage
>>> [i for s, r, i in findall_partial(':', TIME_REGEX_PARTS)] [2, 5] >>> [embed_in_regex(s, r, i) for s, r, i in findall_partial('12:15:2', TIME_REGEX_PARTS)] [(0, '12:15:2[0-9]')] >>> [embed_in_regex(s, r, i) for s, r, i in findall_partial('18:2', TIME_REGEX_PARTS)] [(0, '18:2[0-9]:[0-5][0-9]'), (3, '[0-2][0-9]:18:2[0-9]')] >>> [embed_in_regex(s, r, i) for s, r, i in findall_partial('59:1', TIME_REGEX_PARTS)] [(3, '[0-2][0-9]:59:1[0-9]')]
- pytoolbox.regex.from_path_patterns(patterns, regex=False)[source]¶
Return patterns compiled to regular expressions, if necessary.
If regex is set to False, then any string pattern will be converted from the unix-style wildcard to the regular expression equivalent using
fnatmch.translate()
.Example usage
>>> from pytoolbox.unittest import asserts >>> asserts.list_equal( ... [r.pattern for r in from_path_patterns('*.txt')], ... ['(?s:.*\\.txt)\\Z']) >>> asserts.list_equal( ... [r.pattern for r in from_path_patterns(['text', 'something*'], '*.py')], ... ['text', 'something*']) >>> asserts.list_equal( ... [r.pattern for r in from_path_patterns([re.compile('a?c'), 'fo?'])], ... ['a?c', '(?s:fo.)\\Z']) >>> asserts.list_equal( ... [r.pattern for r in from_path_patterns([re.compile('a?c'), 'foo?'], regex=True)], ... ['a?c', 'foo?'])