Opened 4 years ago

Closed 4 years ago

#31311 closed Cleanup/optimization (fixed)

Unneeded escape sequences in character classes

Reported by: Kimball Leavitt Owned by: nobody
Component: Core (Other) Version: 3.0
Severity: Normal Keywords: validators, regex
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Change History (5)

comment:1 Changed 4 years ago by Carlton Gibson

Has patch: unset
Triage Stage: UnreviewedAccepted

The third example here (L162) is:


I can't see an escape inside a character class there.
Might just be early?

I applied a quick adjustment, which seemed to run OK. (Existing tests passed.)

diff --git a/django/core/ b/django/core/
index dc0519bb6a..c19fa0becd 100644
--- a/django/core/
+++ b/django/core/
@@ -65,7 +65,7 @@ class URLValidator(RegexValidator):
     # IP patterns
     ipv4_re = r'(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}'
-    ipv6_re = r'\[[0-9a-f:\.]+\]'  # (simple regex, validated later)
+    ipv6_re = r'\[[0-9a-f:.]+\]'  # (simple regex, validated later)
     # Host patterns
     hostname_re = r'[a-z' + ul + r'0-9](?:[a-z' + ul + r'0-9-]{0,61}[a-z' + ul + r'0-9])?'
@@ -82,7 +82,7 @@ class URLValidator(RegexValidator):
     host_re = '(' + hostname_re + domain_re + tld_re + '|localhost)'
     regex = _lazy_re_compile(
-        r'^(?:[a-z0-9\.\-\+]*)://'  # scheme is validated separately
+        r'^(?:[a-z0-9.\-+]*)://'  # scheme is validated separately
         r'(?:[^\s:@/]+(?::[^\s:@/]*)?@)?'  # user:pass authentication
         r'(?:' + ipv4_re + '|' + ipv6_re + '|' + host_re + ')'
         r'(?::\d{2,5})?'  # port

Happy to look more closely in a PR.

Thanks for the report.

comment:2 Changed 4 years ago by Kimball Leavitt

Has patch: set

comment:3 Changed 4 years ago by Carlton Gibson

Triage Stage: AcceptedReady for checkin


Yep, this looks fine. Thank you.

comment:4 Changed 4 years ago by Kimball Leavitt

Description: modified (diff)
Triage Stage: Ready for checkinAccepted

Thanks for the review. I submitted a PR for this yesterday -

You're right, the third one was wrong. I made the fix first, then submitted the ticket, so I hastily grabbed the wrong line. I changed it so it's correct (

Ah, you beat me to it lol.

Last edited 4 years ago by Kimball Leavitt (previous) (diff)

comment:5 Changed 4 years ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: newclosed

In 7c6b663:

Fixed #31311 -- Removed unneeded escapes in validator regexes.

Special characters lose their special meaning inside sets of characters.

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

Note: See TracTickets for help on using tickets.
Back to Top