We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Validating and Parsing Email Addresses
Validating and Parsing Email Addresses
Sort by
recency
|
315 Discussions
|
Please Login in order to post a comment
My solution was quite simple and just used regex (as this was what I am practicing). The pattern is specific to the email format they have specified.
[A-Za-z]*?
is a lazy quantifier that will match any alphabetical characters as few times as possible.<[A-Za-z][A-Za-z\0-9_\-\.]*?
is also a lazy quantifier - the key being it specifies the first character must be alphabetical followed by any of the characters in the character class, again as few times as possible.[A-Za-z]+?\.[A-Za-z]{1,3}>
is the final piece and follows the @ symbol. The domain name again is characterized by a lazy quantifier, this time with + to mean there must be some character there. The extension is can be any alphabetical character and must be 1-3 characters long, as per the rules.Ofcource we can do with simple regex but Brute force sol would be:
import re
N = int(input())
for i in range(N): name, email = input().split() pattern="<[a-z][a-zA-Z0-9-._]+@[a-zA-Z]+.[a-zA-Z]{1,3}>" if bool(re.match(pattern, email)): print(name,email)