-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathreg_ex_email_parse.py
63 lines (45 loc) · 1.69 KB
/
reg_ex_email_parse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
You are given names and email addresses. Your task is to print the names and email addresses if they are valid.
A valid email address follows the rules below:
- Email must have three basic components: username @ website name . extension.
- The username can contain: alphanumeric characters, -,. and _.
- The username must start with an English alphabet character.
- The website name contains only English alphabet characters.
- The extension contains only English alphabet characters, and its length can be 1, 2, or 3.
Input Format
name <[email protected]>
The first line contains an integer .
The next lines contains a name and an email address separated by a space.
Constraints
Output Format
Print the valid email addresses only. Print the space separated name and email address on separate lines.
Output the valid results in order of their occurrence.
Sample Input
2
DEXTER <[email protected]>
VIRUS <virus!@variable.:p>
Sample Output
DEXTER <[email protected]>
Explanation
This is a valid email address.
virus!@variable.:p
This is invalid because it contains a ! in the username and a : in the extension.
Bonus
Email.utils()
>>> import email.utils
>>> print email.utils.parseaddr('DOSHI <[email protected]>')
('DOSHI', '[email protected]')
>>> print email.utils.formataddr(('DOSHI', '[email protected]'))
DOSHI <[email protected]>
"""
# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
import email.utils
m = input()
p = [ email.utils.parseaddr(raw_input()) for _ in xrange(m)]
for em in p:
if bool(re.match(r'[a-z]{1}[\w._-]+@[\w]+.\w{1,3}$',em[1])):
print email.utils.formataddr(em)
else:
pass