· 7 years ago · Sep 02, 2018, 08:42 PM
1Python Regular Expressions, find Email Domain in Address
2'blahblah@gmail.com'
3
4'@gmail.com'
5
6.
7
8import re
9test_string = 'blahblah@gmail.com'
10domain = re.search('@*?.', test_string)
11print domain.group()
12
13' # begin to define the pattern I'm looking for (also tell python this is a string)
14
15 @ # find all patterns beginning with the at symbol ("@")
16
17 * # find all characters after ampersand
18
19 ? # find the last character before the period
20
21 # breakout (don't use the next character as a wild card, us it is a string character)
22
23 . # find the "." character
24
25 ' # end definition of the pattern I'm looking for (also tell python this is a string)
26
27 , test string # run the preceding search on the variable "test_string," i.e., 'blahblah@gmail.com'
28
29import re
30s = 'My name is Conrad, and blahblah@gmail.com is my email.'
31domain = re.search("@[w.]+", s)
32print domain.group()
33
34@gmail.com
35
36>>> re.search('@.*', test_string).group()
37'@gmail.com'
38
39>>> '@' + test_string.split('@')[1]
40'@gmail.com'
41
42"@"+'blahblah@gmail.com'.split("@")[-1]
43
44>>> s="bal@gmail.com"
45>>> s[ s.find("@") : ]
46'@gmail.com'
47>>>
48
49f=open("file")
50for line in f:
51 words= line.split()
52 if "@" in words:
53 print "@"+words.split("@")[-1]
54f.close()