Validating and Parsing Email Addresses in Python - HackerRank Solution

 Validating and Parsing Email Addresses in Python - HackerRank Solution
 


Problem :


A valid email address meets the following criteria:
  • It's composed of a username, domain name, and extension assembled in this format: username@domain.extension
  • The username starts with an English alphabetical character, and any subsequent characters consist of one or more of the following: alphanumeric characters, -,., and _.
  • The domain and extension contain only English alphabetical characters.
  • The extension is 1, 2, or 3 characters in length.
Given n pairs of names and email addresses as input, print each name and email address pair having a valid email address on a new line.

Hint : Try using Email.utils() to complete this challenge. For example, this code:
import email.utils
print email.utils.parseaddr('DOSHI <DOSHI@hackerrank.com>')
print email.utils.formataddr(('DOSHI', 'DOSHI@hackerrank.com'))

produces this output:
('DOSHI', 'DOSHI@hackerrank.com')
DOSHI <DOSHI@hackerrank.com>



Input Format :

The first line contains a single integer, n, denoting the number of email address.
Each line i of the n subsequent lines contains a name and an email address as two space-separated values following this format:
name <user@email.com>

Constraints :

  • 0 < N < 100

Output Format :

Print the space-separated name and email address pairs containing valid email addresses only. Each pair must be printed on a new line in the following format:
name <user@email.com>

You must print each valid email address in the same order as it was received as input.



Sample Input :

2  
DEXTER <dexter@hotmail.com>
VIRUS <virus!@variable.:p>

Sample Output :

DEXTER <dexter@hotmail.com>

Explanation :

dexter@hotmail.com is a valid email address, so we print the name and email address pair received as input on a new line.
virus!@variable.:p is not a valid email address because the username contains an exclamation point (!) and the extension contains a colon (:). As this email is not valid, we print nothing.



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Validating and Parsing Email Addresses in Python - Hacker Rank Solution
# Python 3
# Enter your code here. Read input from STDIN. Print output to STDOUT
# Validating and Parsing Email Addresses in Python - Hacker Rank Solution START
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)
# Validating and Parsing Email Addresses in Python - Hacker Rank Solution END





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
4 Comments
  • Unknown
    Unknown Tuesday, March 16, 2021

    how to code run in compiler i cant under stands

  • Mack
    Mack Tuesday, September 14, 2021

    Hi, I want to express my gratitude to you for sharing this fascinating information. It's amazing that we now have the ability to share our thoughts. Share such information with us through blogs and internet services.
    Visit site

  • klantenservicehelp
    klantenservicehelp Thursday, September 16, 2021

    Good Information, reading this actually felt like this is helpful information, thanks For sharing knowledgeable information with us! keep posting.
    visit site

  • Angel charls
    Angel charls Saturday, June 25, 2022

    Thank you for giving important information. This information is very much important for me. I have a this related website. If you want more information about this topic then click here.
    PayPal Bellen Nederland

Add Comment
comment url