diff --git a/django/utils/html.py b/django/utils/html.py
index 3bc02b8dd3..bd58317a79 100644
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -283,8 +283,9 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
middle_unescaped = html.unescape(middle)
stripped = middle_unescaped.rstrip(TRAILING_PUNCTUATION_CHARS)
if middle_unescaped != stripped:
- trail = middle[len(stripped):] + trail
- middle = middle[:len(stripped) - len(middle_unescaped)]
+ punctuation_count = len(middle_unescaped) - len(stripped)
+ trail = middle[-punctuation_count:] + trail
+ middle = middle[:-punctuation_count]
trimmed_something = True
return lead, middle, trail
diff --git a/tests/utils_tests/test_html.py b/tests/utils_tests/test_html.py
index 30f5ba68e8..909620ea3f 100644
--- a/tests/utils_tests/test_html.py
+++ b/tests/utils_tests/test_html.py
@@ -250,6 +250,10 @@ class TestUtilsHtml(SimpleTestCase):
'Search for google.com/?q=! and see.',
'Search for google.com/?q=! and see.'
),
+ (
+ 'Search for google.com/?q=1<! and see.',
+ 'Search for google.com/?q=1<! and see.'
+ ),
(
lazystr('Search for google.com/?q=!'),
'Search for google.com/?q=!'