Skip to content

Commit a800535

Browse files
authored
Use end-of-string matcher instead of end-of-line matcher in regular expressions (#37)
1 parent dbd730b commit a800535

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

email_validator/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def validate_email_local_part(local, allow_smtputf8=True, allow_empty_local=Fals
128128
raise EmailSyntaxError("The email address is too long before the @-sign.")
129129

130130
# Check the local part against the regular expression for the older ASCII requirements.
131-
m = re.match(DOT_ATOM_TEXT + "$", local)
131+
m = re.match(DOT_ATOM_TEXT + "\\Z", local)
132132
if m:
133133
# Return the local part unchanged and flag that SMTPUTF8 is not needed.
134134
return {
@@ -138,7 +138,7 @@ def validate_email_local_part(local, allow_smtputf8=True, allow_empty_local=Fals
138138

139139
else:
140140
# The local part failed the ASCII check. Now try the extended internationalized requirements.
141-
m = re.match(DOT_ATOM_TEXT_UTF8 + "$", local)
141+
m = re.match(DOT_ATOM_TEXT_UTF8 + "\\Z", local)
142142
if not m:
143143
# It's not a valid internationalized address either. Report which characters were not valid.
144144
bad_chars = ', '.join(sorted(set(
@@ -226,15 +226,15 @@ def validate_email_domain_part(domain):
226226

227227
# Check the regular expression. This is probably entirely redundant
228228
# with idna.decode, which also checks this format.
229-
m = re.match(DOT_ATOM_TEXT + "$", domain)
229+
m = re.match(DOT_ATOM_TEXT + "\\Z", domain)
230230
if not m:
231231
raise EmailSyntaxError("The email address contains invalid characters after the @-sign.")
232232

233233
# All publicly deliverable addresses have domain named with at least
234234
# one period. We also know that all TLDs end with a letter.
235235
if "." not in domain:
236236
raise EmailSyntaxError("The domain name %s is not valid. It should have a period." % domain_i18n)
237-
if not re.search(r"[A-Za-z]$", domain):
237+
if not re.search(r"[A-Za-z]\Z", domain):
238238
raise EmailSyntaxError(
239239
"The domain name %s is not valid. It is not within a valid top-level domain." % domain_i18n
240240
)

tests/test_main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,22 @@ def test_email_valid(email_input, output):
210210
'The domain name baddash.-a.com contains invalid characters (Label must not start or end with a hyphen).'),
211211
('my@baddash.b-.com',
212212
'The domain name baddash.b-.com contains invalid characters (Label must not start or end with a hyphen).'),
213+
('my@example.com\n',
214+
'The domain name example.com\n contains invalid characters (Codepoint U+000A at position 4 of '
215+
'\'com\\n\' not allowed).'),
216+
('my@example\n.com',
217+
'The domain name example\n.com contains invalid characters (Codepoint U+000A at position 8 of '
218+
'\'example\\n\' not allowed).'),
213219
('.leadingdot@domain.com', 'The email address contains invalid characters before the @-sign: ..'),
214220
('..twodots@domain.com', 'The email address contains invalid characters before the @-sign: ..'),
215221
('twodots..here@domain.com', 'The email address contains invalid characters before the @-sign: ..'),
216222
('me@⒈wouldbeinvalid.com',
217223
"The domain name ⒈wouldbeinvalid.com contains invalid characters (Codepoint U+2488 not allowed "
218224
"at position 1 in '⒈wouldbeinvalid.com')."),
219225
('@example.com', 'There must be something before the @-sign.'),
226+
('\nmy@example.com', 'The email address contains invalid characters before the @-sign: \n.'),
227+
('m\ny@example.com', 'The email address contains invalid characters before the @-sign: \n.'),
228+
('my\n@example.com', 'The email address contains invalid characters before the @-sign: \n.'),
220229
],
221230
)
222231
def test_email_invalid(email_input, error_msg):

0 commit comments

Comments
 (0)