· 8 years ago · Sep 23, 2017, 02:34 PM
1import string
2EMAIL_CHARS = string.ascii_letters + string.digits + '.'
3
4def find_name_start(text, at_index):
5 first_index = 0
6 for index in range(at_index-1, -1, -1):
7 if text[index] == at_index:
8 break
9
10 if text[index] not in EMAIL_CHARS:
11 first_index = index + 1
12 break
13 return first_index
14
15def find_name_end(text, at_index):
16 first_index = 0
17 for index in range(at_index+1,len(text)):
18
19 #if text[index] == at_index:
20 # break
21 if text[index] not in EMAIL_CHARS:
22 first_index = index + 1
23 break
24 if index == len(text)-1:
25 first_index = index + 1
26 break
27 # if text[index] == 0:
28
29 return first_index
30
31def find_email(text):
32 at_index = text.find('@')
33
34
35 email_list = []
36 while at_index > -1 :
37 name_start = find_name_start(text, at_index)
38 name_end = find_name_end(text, at_index)
39
40 email = text[name_start : name_end]
41 email_list.append(email)
42
43 if name_start == at_index:
44 email_list.remove(email)
45 #if email_list == []:
46 # print "Please, type in an email address"
47 # return
48
49 if name_end - 2 == at_index and name_end == 0:
50 email_list.remove(email)
51
52 # if name_end == 0:
53 #email_list.remove(email)
54
55
56 at_index = text.find('@',at_index+1)
57
58
59 return email_list
60
61
62
63#print find_email("@gmail.com olle@gmail.com")
64#print find_email("olle@gmail.com @gmail.com")
65#print find_email("olle@ olle@gmail.com")
66#print find_email("olle@gmail.com olle@")
67#print find_email("@olle olle@gmail.com @olle")
68#print find_email("@gmail.com olle@gmail.com @gmail.com")
69#print find_email("")
70#print find_email("hej ehj olle@gmail.com @sfeeahfaf 1234 {[]9)8?#&")
71print find_email("@ olle@gmail.com")