Skip to content

Commit

Permalink
Removed unneeded escapes in regexes.
Browse files Browse the repository at this point in the history
Special characters lose their special meaning inside sets of characters.

"-" lose its special meaning if it's placed as the first or last
character.

Follow up to 7c6b663.
  • Loading branch information
felixxm authored Aug 2, 2023
1 parent 7cd187a commit 9b9c805
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion django/contrib/admindocs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
name="django-admindocs-models-index",
),
re_path(
r"^models/(?P<app_label>[^\.]+)\.(?P<model_name>[^/]+)/$",
r"^models/(?P<app_label>[^.]+)\.(?P<model_name>[^/]+)/$",
views.ModelDetailView.as_view(),
name="django-admindocs-models-detail",
),
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/flatpages/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FlatpageForm(forms.ModelForm):
url = forms.RegexField(
label=_("URL"),
max_length=100,
regex=r"^[-\w/\.~]+$",
regex=r"^[-\w/.~]+$",
help_text=_(
"Example: “/about/contact/”. Make sure to have leading and trailing "
"slashes."
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/gis/db/models/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class OverlapsLookup(GISLookup):
class RelateLookup(GISLookup):
lookup_name = "relate"
sql_template = "%(func)s(%(lhs)s, %(rhs)s, %%s)"
pattern_regex = _lazy_re_compile(r"^[012TF\*]{9}$")
pattern_regex = _lazy_re_compile(r"^[012TF*]{9}$")

def process_rhs(self, compiler, connection):
# Check the pattern argument
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/gis/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
r"(?P<wkt>"
r"(?P<type>POINT|LINESTRING|LINEARRING|POLYGON|MULTIPOINT|"
r"MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)"
r"[ACEGIMLONPSRUTYZ0-9,\.\-\+\(\) ]+)$",
r"[ACEGIMLONPSRUTYZ0-9,.+() -]+)$",
re.I,
)
json_regex = _lazy_re_compile(r"^(\s+)?\{.*}(\s+)?$", re.DOTALL)
6 changes: 3 additions & 3 deletions django/contrib/staticfiles/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ class HashedFilesMixin:
(
(
r"""(?P<matched>import(?s:(?P<import>[\s\{].*?))"""
r"""\s*from\s*['"](?P<url>[\.\/].*?)["']\s*;)"""
r"""\s*from\s*['"](?P<url>[./].*?)["']\s*;)"""
),
"""import%(import)s from "%(url)s";""",
),
(
(
r"""(?P<matched>export(?s:(?P<exports>[\s\{].*?))"""
r"""\s*from\s*["'](?P<url>[\.\/].*?)["']\s*;)"""
r"""\s*from\s*["'](?P<url>[./].*?)["']\s*;)"""
),
"""export%(exports)s from "%(url)s";""",
),
(
r"""(?P<matched>import\s*['"](?P<url>[\.\/].*?)["']\s*;)""",
r"""(?P<matched>import\s*['"](?P<url>[./].*?)["']\s*;)""",
"""import"%(url)s";""",
),
(
Expand Down
2 changes: 1 addition & 1 deletion django/db/models/functions/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Collate(Func):
allowed_default = False
# Inspired from
# https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
collation_re = _lazy_re_compile(r"^[\w\-]+$")
collation_re = _lazy_re_compile(r"^[\w-]+$")

def __init__(self, expression, collation):
if not (collation and self.collation_re.match(collation)):
Expand Down
2 changes: 1 addition & 1 deletion django/db/models/sql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

# Inspired from
# https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
EXPLAIN_OPTIONS_PATTERN = _lazy_re_compile(r"[\w\-]+")
EXPLAIN_OPTIONS_PATTERN = _lazy_re_compile(r"[\w-]+")


def get_field_names_from_opts(opts):
Expand Down
2 changes: 1 addition & 1 deletion django/template/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def find_filter(self, filter_name):
)?
)""" % {
"constant": constant_string,
"num": r"[-+\.]?\d[\d\.e]*",
"num": r"[-+.]?\d[\d.e]*",
"var_chars": r"\w\.",
"filter_sep": re.escape(FILTER_SEPARATOR),
"arg_sep": re.escape(FILTER_ARGUMENT_SEPARATOR),
Expand Down
14 changes: 7 additions & 7 deletions django/utils/dateparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

time_re = _lazy_re_compile(
r"(?P<hour>\d{1,2}):(?P<minute>\d{1,2})"
r"(?::(?P<second>\d{1,2})(?:[\.,](?P<microsecond>\d{1,6})\d{0,6})?)?$"
r"(?::(?P<second>\d{1,2})(?:[.,](?P<microsecond>\d{1,6})\d{0,6})?)?$"
)

datetime_re = _lazy_re_compile(
r"(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})"
r"[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})"
r"(?::(?P<second>\d{1,2})(?:[\.,](?P<microsecond>\d{1,6})\d{0,6})?)?"
r"(?::(?P<second>\d{1,2})(?:[.,](?P<microsecond>\d{1,6})\d{0,6})?)?"
r"\s*(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$"
)

Expand All @@ -31,7 +31,7 @@
r"((?:(?P<hours>\d+):)(?=\d+:\d+))?"
r"(?:(?P<minutes>\d+):)?"
r"(?P<seconds>\d+)"
r"(?:[\.,](?P<microseconds>\d{1,6})\d{0,6})?"
r"(?:[.,](?P<microseconds>\d{1,6})\d{0,6})?"
r"$"
)

Expand All @@ -40,11 +40,11 @@
iso8601_duration_re = _lazy_re_compile(
r"^(?P<sign>[-+]?)"
r"P"
r"(?:(?P<days>\d+([\.,]\d+)?)D)?"
r"(?:(?P<days>\d+([.,]\d+)?)D)?"
r"(?:T"
r"(?:(?P<hours>\d+([\.,]\d+)?)H)?"
r"(?:(?P<minutes>\d+([\.,]\d+)?)M)?"
r"(?:(?P<seconds>\d+([\.,]\d+)?)S)?"
r"(?:(?P<hours>\d+([.,]\d+)?)H)?"
r"(?:(?P<minutes>\d+([.,]\d+)?)M)?"
r"(?:(?P<seconds>\d+([.,]\d+)?)S)?"
r")?"
r"$"
)
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
linkcheck_ignore = [
# Special-use addresses and domain names. (RFC 6761/6890)
r"^https?://(?:127\.0\.0\.1|\[::1\])(?::\d+)?/",
r"^https?://(?:[^/\.]+\.)*example\.(?:com|net|org)(?::\d+)?/",
r"^https?://(?:[^/\.]+\.)*(?:example|invalid|localhost|test)(?::\d+)?/",
r"^https?://(?:[^/.]+\.)*example\.(?:com|net|org)(?::\d+)?/",
r"^https?://(?:[^/.]+\.)*(?:example|invalid|localhost|test)(?::\d+)?/",
# Pages that are inaccessible because they require authentication.
r"^https://github\.com/[^/]+/[^/]+/fork",
r"^https://code\.djangoproject\.com/github/login",
Expand Down
22 changes: 11 additions & 11 deletions tests/validators/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,24 +690,24 @@ class TestValidatorEquality(TestCase):

def test_regex_equality(self):
self.assertEqual(
RegexValidator(r"^(?:[a-z0-9\.\-]*)://"),
RegexValidator(r"^(?:[a-z0-9\.\-]*)://"),
RegexValidator(r"^(?:[a-z0-9.-]*)://"),
RegexValidator(r"^(?:[a-z0-9.-]*)://"),
)
self.assertNotEqual(
RegexValidator(r"^(?:[a-z0-9\.\-]*)://"),
RegexValidator(r"^(?:[0-9\.\-]*)://"),
RegexValidator(r"^(?:[a-z0-9.-]*)://"),
RegexValidator(r"^(?:[0-9.-]*)://"),
)
self.assertEqual(
RegexValidator(r"^(?:[a-z0-9\.\-]*)://", "oh noes", "invalid"),
RegexValidator(r"^(?:[a-z0-9\.\-]*)://", "oh noes", "invalid"),
RegexValidator(r"^(?:[a-z0-9.-]*)://", "oh noes", "invalid"),
RegexValidator(r"^(?:[a-z0-9.-]*)://", "oh noes", "invalid"),
)
self.assertNotEqual(
RegexValidator(r"^(?:[a-z0-9\.\-]*)://", "oh", "invalid"),
RegexValidator(r"^(?:[a-z0-9\.\-]*)://", "oh noes", "invalid"),
RegexValidator(r"^(?:[a-z0-9.-]*)://", "oh", "invalid"),
RegexValidator(r"^(?:[a-z0-9.-]*)://", "oh noes", "invalid"),
)
self.assertNotEqual(
RegexValidator(r"^(?:[a-z0-9\.\-]*)://", "oh noes", "invalid"),
RegexValidator(r"^(?:[a-z0-9\.\-]*)://"),
RegexValidator(r"^(?:[a-z0-9.-]*)://", "oh noes", "invalid"),
RegexValidator(r"^(?:[a-z0-9.-]*)://"),
)

self.assertNotEqual(
Expand All @@ -721,7 +721,7 @@ def test_regex_equality(self):
)

def test_regex_equality_nocache(self):
pattern = r"^(?:[a-z0-9\.\-]*)://"
pattern = r"^(?:[a-z0-9.-]*)://"
left = RegexValidator(pattern)
re.purge()
right = RegexValidator(pattern)
Expand Down

0 comments on commit 9b9c805

Please sign in to comment.