Refs #32499 -- Added metacharacters helper for simplify_regex().

This commit is contained in:
Nick Pope 2021-03-02 12:46:14 +05:30 committed by Mariusz Felisiak
parent 221b2f85fe
commit 27189af8cf
2 changed files with 7 additions and 3 deletions

View File

@ -142,6 +142,11 @@ named_group_matcher = _lazy_re_compile(r'\(\?P(<\w+>)')
unnamed_group_matcher = _lazy_re_compile(r'\(')
def replace_metacharacters(pattern):
"""Remove unescaped metacharacters from the pattern."""
return pattern.replace('^', '').replace('$', '').replace('?', '')
def replace_named_groups(pattern):
r"""
Find named groups in `pattern` and replace them with the group name. E.g.,

View File

@ -8,7 +8,7 @@ from django.contrib import admin
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.admindocs import utils
from django.contrib.admindocs.utils import (
replace_named_groups, replace_unnamed_groups,
replace_metacharacters, replace_named_groups, replace_unnamed_groups,
)
from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
from django.db import models
@ -412,8 +412,7 @@ def simplify_regex(pattern):
"""
pattern = replace_named_groups(pattern)
pattern = replace_unnamed_groups(pattern)
# clean up any outstanding regex-y characters.
pattern = pattern.replace('^', '').replace('$', '').replace('?', '')
pattern = replace_metacharacters(pattern)
if not pattern.startswith('/'):
pattern = '/' + pattern
return pattern