Skip to content

Commit 9b9c805

Browse files
authored
Removed unneeded escapes in regexes.
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.
1 parent 7cd187a commit 9b9c805

File tree

11 files changed

+30
-30
lines changed

11 files changed

+30
-30
lines changed

django/contrib/admindocs/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
name="django-admindocs-models-index",
3939
),
4040
re_path(
41-
r"^models/(?P<app_label>[^\.]+)\.(?P<model_name>[^/]+)/$",
41+
r"^models/(?P<app_label>[^.]+)\.(?P<model_name>[^/]+)/$",
4242
views.ModelDetailView.as_view(),
4343
name="django-admindocs-models-detail",
4444
),

django/contrib/flatpages/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class FlatpageForm(forms.ModelForm):
1010
url = forms.RegexField(
1111
label=_("URL"),
1212
max_length=100,
13-
regex=r"^[-\w/\.~]+$",
13+
regex=r"^[-\w/.~]+$",
1414
help_text=_(
1515
"Example: “/about/contact/”. Make sure to have leading and trailing "
1616
"slashes."

django/contrib/gis/db/models/lookups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class OverlapsLookup(GISLookup):
273273
class RelateLookup(GISLookup):
274274
lookup_name = "relate"
275275
sql_template = "%(func)s(%(lhs)s, %(rhs)s, %%s)"
276-
pattern_regex = _lazy_re_compile(r"^[012TF\*]{9}$")
276+
pattern_regex = _lazy_re_compile(r"^[012TF*]{9}$")
277277

278278
def process_rhs(self, compiler, connection):
279279
# Check the pattern argument

django/contrib/gis/geometry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
r"(?P<wkt>"
1212
r"(?P<type>POINT|LINESTRING|LINEARRING|POLYGON|MULTIPOINT|"
1313
r"MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)"
14-
r"[ACEGIMLONPSRUTYZ0-9,\.\-\+\(\) ]+)$",
14+
r"[ACEGIMLONPSRUTYZ0-9,.+() -]+)$",
1515
re.I,
1616
)
1717
json_regex = _lazy_re_compile(r"^(\s+)?\{.*}(\s+)?$", re.DOTALL)

django/contrib/staticfiles/storage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ class HashedFilesMixin:
5454
(
5555
(
5656
r"""(?P<matched>import(?s:(?P<import>[\s\{].*?))"""
57-
r"""\s*from\s*['"](?P<url>[\.\/].*?)["']\s*;)"""
57+
r"""\s*from\s*['"](?P<url>[./].*?)["']\s*;)"""
5858
),
5959
"""import%(import)s from "%(url)s";""",
6060
),
6161
(
6262
(
6363
r"""(?P<matched>export(?s:(?P<exports>[\s\{].*?))"""
64-
r"""\s*from\s*["'](?P<url>[\.\/].*?)["']\s*;)"""
64+
r"""\s*from\s*["'](?P<url>[./].*?)["']\s*;)"""
6565
),
6666
"""export%(exports)s from "%(url)s";""",
6767
),
6868
(
69-
r"""(?P<matched>import\s*['"](?P<url>[\.\/].*?)["']\s*;)""",
69+
r"""(?P<matched>import\s*['"](?P<url>[./].*?)["']\s*;)""",
7070
"""import"%(url)s";""",
7171
),
7272
(

django/db/models/functions/comparison.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class Collate(Func):
108108
allowed_default = False
109109
# Inspired from
110110
# https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
111-
collation_re = _lazy_re_compile(r"^[\w\-]+$")
111+
collation_re = _lazy_re_compile(r"^[\w-]+$")
112112

113113
def __init__(self, expression, collation):
114114
if not (collation and self.collation_re.match(collation)):

django/db/models/sql/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

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

5757

5858
def get_field_names_from_opts(opts):

django/template/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ def find_filter(self, filter_name):
632632
)?
633633
)""" % {
634634
"constant": constant_string,
635-
"num": r"[-+\.]?\d[\d\.e]*",
635+
"num": r"[-+.]?\d[\d.e]*",
636636
"var_chars": r"\w\.",
637637
"filter_sep": re.escape(FILTER_SEPARATOR),
638638
"arg_sep": re.escape(FILTER_ARGUMENT_SEPARATOR),

django/utils/dateparse.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

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

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

@@ -31,7 +31,7 @@
3131
r"((?:(?P<hours>\d+):)(?=\d+:\d+))?"
3232
r"(?:(?P<minutes>\d+):)?"
3333
r"(?P<seconds>\d+)"
34-
r"(?:[\.,](?P<microseconds>\d{1,6})\d{0,6})?"
34+
r"(?:[.,](?P<microseconds>\d{1,6})\d{0,6})?"
3535
r"$"
3636
)
3737

@@ -40,11 +40,11 @@
4040
iso8601_duration_re = _lazy_re_compile(
4141
r"^(?P<sign>[-+]?)"
4242
r"P"
43-
r"(?:(?P<days>\d+([\.,]\d+)?)D)?"
43+
r"(?:(?P<days>\d+([.,]\d+)?)D)?"
4444
r"(?:T"
45-
r"(?:(?P<hours>\d+([\.,]\d+)?)H)?"
46-
r"(?:(?P<minutes>\d+([\.,]\d+)?)M)?"
47-
r"(?:(?P<seconds>\d+([\.,]\d+)?)S)?"
45+
r"(?:(?P<hours>\d+([.,]\d+)?)H)?"
46+
r"(?:(?P<minutes>\d+([.,]\d+)?)M)?"
47+
r"(?:(?P<seconds>\d+([.,]\d+)?)S)?"
4848
r")?"
4949
r"$"
5050
)

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
linkcheck_ignore = [
5555
# Special-use addresses and domain names. (RFC 6761/6890)
5656
r"^https?://(?:127\.0\.0\.1|\[::1\])(?::\d+)?/",
57-
r"^https?://(?:[^/\.]+\.)*example\.(?:com|net|org)(?::\d+)?/",
58-
r"^https?://(?:[^/\.]+\.)*(?:example|invalid|localhost|test)(?::\d+)?/",
57+
r"^https?://(?:[^/.]+\.)*example\.(?:com|net|org)(?::\d+)?/",
58+
r"^https?://(?:[^/.]+\.)*(?:example|invalid|localhost|test)(?::\d+)?/",
5959
# Pages that are inaccessible because they require authentication.
6060
r"^https://github\.com/[^/]+/[^/]+/fork",
6161
r"^https://code\.djangoproject\.com/github/login",

tests/validators/tests.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -690,24 +690,24 @@ class TestValidatorEquality(TestCase):
690690

691691
def test_regex_equality(self):
692692
self.assertEqual(
693-
RegexValidator(r"^(?:[a-z0-9\.\-]*)://"),
694-
RegexValidator(r"^(?:[a-z0-9\.\-]*)://"),
693+
RegexValidator(r"^(?:[a-z0-9.-]*)://"),
694+
RegexValidator(r"^(?:[a-z0-9.-]*)://"),
695695
)
696696
self.assertNotEqual(
697-
RegexValidator(r"^(?:[a-z0-9\.\-]*)://"),
698-
RegexValidator(r"^(?:[0-9\.\-]*)://"),
697+
RegexValidator(r"^(?:[a-z0-9.-]*)://"),
698+
RegexValidator(r"^(?:[0-9.-]*)://"),
699699
)
700700
self.assertEqual(
701-
RegexValidator(r"^(?:[a-z0-9\.\-]*)://", "oh noes", "invalid"),
702-
RegexValidator(r"^(?:[a-z0-9\.\-]*)://", "oh noes", "invalid"),
701+
RegexValidator(r"^(?:[a-z0-9.-]*)://", "oh noes", "invalid"),
702+
RegexValidator(r"^(?:[a-z0-9.-]*)://", "oh noes", "invalid"),
703703
)
704704
self.assertNotEqual(
705-
RegexValidator(r"^(?:[a-z0-9\.\-]*)://", "oh", "invalid"),
706-
RegexValidator(r"^(?:[a-z0-9\.\-]*)://", "oh noes", "invalid"),
705+
RegexValidator(r"^(?:[a-z0-9.-]*)://", "oh", "invalid"),
706+
RegexValidator(r"^(?:[a-z0-9.-]*)://", "oh noes", "invalid"),
707707
)
708708
self.assertNotEqual(
709-
RegexValidator(r"^(?:[a-z0-9\.\-]*)://", "oh noes", "invalid"),
710-
RegexValidator(r"^(?:[a-z0-9\.\-]*)://"),
709+
RegexValidator(r"^(?:[a-z0-9.-]*)://", "oh noes", "invalid"),
710+
RegexValidator(r"^(?:[a-z0-9.-]*)://"),
711711
)
712712

713713
self.assertNotEqual(
@@ -721,7 +721,7 @@ def test_regex_equality(self):
721721
)
722722

723723
def test_regex_equality_nocache(self):
724-
pattern = r"^(?:[a-z0-9\.\-]*)://"
724+
pattern = r"^(?:[a-z0-9.-]*)://"
725725
left = RegexValidator(pattern)
726726
re.purge()
727727
right = RegexValidator(pattern)

0 commit comments

Comments
 (0)