Refs #32499 -- Fixed escaped metacharacters in simplify_regex().
This commit is contained in:
parent
9f7809ece3
commit
85540da930
|
@ -144,7 +144,11 @@ unnamed_group_matcher = _lazy_re_compile(r'\(')
|
|||
|
||||
def replace_metacharacters(pattern):
|
||||
"""Remove unescaped metacharacters from the pattern."""
|
||||
return pattern.replace('^', '').replace('$', '').replace('?', '')
|
||||
return re.sub(
|
||||
r'((?:^|(?<!\\))(?:\\\\)*)(\\?)([?^$])',
|
||||
lambda m: m[1] + m[3] if m[2] else m[1],
|
||||
pattern,
|
||||
)
|
||||
|
||||
|
||||
def replace_named_groups(pattern):
|
||||
|
|
|
@ -409,6 +409,34 @@ class AdminDocViewFunctionsTests(SimpleTestCase):
|
|||
(r'a???', '/a'),
|
||||
# Multiple mixed metacharacters.
|
||||
(r'^a/?$', '/a/'),
|
||||
# Escaped single metacharacters.
|
||||
(r'\^a', r'/^a'),
|
||||
(r'\\^a', r'/\\a'),
|
||||
(r'\\\^a', r'/\\^a'),
|
||||
(r'\\\\^a', r'/\\\\a'),
|
||||
(r'\\\\\^a', r'/\\\\^a'),
|
||||
(r'a\$', r'/a$'),
|
||||
(r'a\\$', r'/a\\'),
|
||||
(r'a\\\$', r'/a\\$'),
|
||||
(r'a\\\\$', r'/a\\\\'),
|
||||
(r'a\\\\\$', r'/a\\\\$'),
|
||||
(r'a\?', r'/a?'),
|
||||
(r'a\\?', r'/a\\'),
|
||||
(r'a\\\?', r'/a\\?'),
|
||||
(r'a\\\\?', r'/a\\\\'),
|
||||
(r'a\\\\\?', r'/a\\\\?'),
|
||||
# Escaped mixed metacharacters.
|
||||
(r'^a\?$', r'/a?'),
|
||||
(r'^a\\?$', r'/a\\'),
|
||||
(r'^a\\\?$', r'/a\\?'),
|
||||
(r'^a\\\\?$', r'/a\\\\'),
|
||||
(r'^a\\\\\?$', r'/a\\\\?'),
|
||||
# Adjacent escaped metacharacters.
|
||||
(r'^a\?\$', r'/a?$'),
|
||||
(r'^a\\?\\$', r'/a\\\\'),
|
||||
(r'^a\\\?\\\$', r'/a\\?\\$'),
|
||||
(r'^a\\\\?\\\\$', r'/a\\\\\\\\'),
|
||||
(r'^a\\\\\?\\\\\$', r'/a\\\\?\\\\$'),
|
||||
)
|
||||
for pattern, output in tests:
|
||||
with self.subTest(pattern=pattern):
|
||||
|
|
Loading…
Reference in New Issue