Input Format
The first line contains a single integer, , denoting the number of email address.
Each line  of the  subsequent lines contains a name and an email address as two space-separated values following this format:
name <user@email.com>
Constraints
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.

hackerrank validating and parsing email addresses problem solution in python

Problem solution in Python 3 programming language.

import re
from email.utils import *
for i in range(int(input())):
    email = parseaddr(input())
    # print email[1]
    # \w is equivalent to a-zA-Z_
    if bool(re.search(r'^[a-zA-Z][\w\-\.]*@[A-Za-z]+\.[a-zA-Z]{1,3}$', email[1])):
        print(formataddr(email))

Second solution

# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
n = int(input())
for _ in range(n):
    x, y = input().split(' ')
    m = re.match(r'<[A-Za-z](\w|-|\.|_)+@[A-Za-z]+\.[A-Za-z]{1,3}>', y)
    if m:
        print(x,y)

Problem solution in Python 2 programming language.

import re
from email.utils import *
for i in range(int(raw_input())):
    email = parseaddr(raw_input())
    # print email[1]
    # \w is equivalent to a-zA-Z_
    if bool(re.search(r'^[a-zA-Z][\w\-\.]*@[A-Za-z]+\.[a-zA-Z]{1,3}$', email[1])):
        print formataddr(email)