Skip to content

Commit 2566808

Browse files
authored
Update emails_mert_can_fidan.py
I make it string validation with regex instead of simply check @ character.
1 parent 166f0b7 commit 2566808

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Week05/emails_mert_can_fidan.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
class Emails(list):
24
def __init__(self, data: [str]) -> None:
35
self.data = list(set(data))
@@ -10,8 +12,18 @@ def __str__(self):
1012
return f"Emails: {self.data}"
1113

1214
def validate(self, data: [str]) -> None:
15+
# Validate email addresses according to the pattern used by Gmail.
16+
# The local part (before the @) can contain:
17+
# - Alphanumeric characters
18+
# - Periods (.) but not consecutively or at the start/end
19+
# The domain part (after the @) can contain:
20+
# - Alphanumeric characters
21+
# - Periods (.) followed by at least two alphabetic characters
22+
# This pattern also supports subdomains.
23+
regex_pattern = r"^[A-Za-z0-9]+(?:[.][A-Za-z0-9]+)*@[A-Za-z0-9]+(?:\.[A-Za-z]{2,}){1,2}$"
24+
1325
for email in data:
1426
if isinstance(email, int):
1527
raise ValueError("Only string values accepted!")
16-
if "@" not in email:
28+
if not re.match(regex_pattern, email):
1729
raise ValueError("This is not an email adress!")

0 commit comments

Comments
 (0)