1 | # -*- coding: cp1251 -*-
|
---|
2 |
|
---|
3 | import re
|
---|
4 | """
|
---|
5 | # https://github.com/django/django/blob/master/django/core/validators.py#L161
|
---|
6 | # Original incorrect rexexp:
|
---|
7 |
|
---|
8 | email_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
|
---|
16 | email_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 |
|
---|
24 | mails = [
|
---|
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 |
|
---|
33 | for 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 | """
|
---|
43 | Incorrect:
|
---|
44 | vadim@example.com ..... matched
|
---|
45 | vadim@example_com ..... non-matched
|
---|
46 | vadim@127.0.0.1 ..... non-matched
|
---|
47 | vadim@192.168.1.106 ..... non-matched
|
---|
48 | vadim@localhost ..... non-matched
|
---|
49 | vadim@localhost.localdomain ..... non-matched
|
---|
50 | user_name@123.123.123.12 ..... non-matched
|
---|
51 |
|
---|
52 | Correct:
|
---|
53 | vadim@example.com ..... matched
|
---|
54 | vadim@example_com ..... non-matched
|
---|
55 | vadim@127.0.0.1 ..... matched
|
---|
56 | vadim@192.168.1.106 ..... matched
|
---|
57 | vadim@localhost ..... non-matched
|
---|
58 | vadim@localhost.localdomain ..... non-matched
|
---|
59 | user_name@123.123.123.12 ..... matched
|
---|
60 | """
|
---|
61 |
|
---|