Ticket #17483: email.py

File email.py, 1.9 KB (added by vadimov@…, 13 years ago)

Example

Line 
1# -*- coding: cp1251 -*-
2
3import re
4"""
5# https://github.com/django/django/blob/master/django/core/validators.py#L161
6# Original incorrect rexexp:
7
8email_re = re.compile(
9 r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom
10 r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
11 r')@((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$)' # domain
12 r'|\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$', re.IGNORECASE) # literal form, ipv4 address (SMTP 4.1.3)
13"""
14
15# Correct regexp
16email_re = re.compile(
17 r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom
18 r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
19 r')@(((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$)' # domain
20 r'|(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$)', re.IGNORECASE) # literal form, ipv4 address (SMTP 4.1.3)
21
22
23
24mails = [
25"vadim@example.com",
26"vadim@example_com",
27"vadim@127.0.0.1",
28"vadim@192.168.1.106",
29"vadim@localhost",
30"vadim@localhost.localdomain",
31"user_name@123.123.123.12"]
32
33for i in mails:
34 m = re.match(email_re, i)
35 print i, "."*5,
36 if m:
37 print "matched"
38 else:
39 print "non-matched"
40
41
42"""
43Incorrect:
44vadim@example.com ..... matched
45vadim@example_com ..... non-matched
46vadim@127.0.0.1 ..... non-matched
47vadim@192.168.1.106 ..... non-matched
48vadim@localhost ..... non-matched
49vadim@localhost.localdomain ..... non-matched
50user_name@123.123.123.12 ..... non-matched
51
52Correct:
53vadim@example.com ..... matched
54vadim@example_com ..... non-matched
55vadim@127.0.0.1 ..... matched
56vadim@192.168.1.106 ..... matched
57vadim@localhost ..... non-matched
58vadim@localhost.localdomain ..... non-matched
59user_name@123.123.123.12 ..... matched
60"""
61
Back to Top