-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregEx.py
34 lines (31 loc) · 955 Bytes
/
regEx.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
import re
# using re.search() like startswith() returns a True/False depending on whether the string matches
# Matching and Extracting Data using re.findall() to match strings to be extracted.
x = 'my 2 favorite numbers are 19 and 42'
y = re.findall('[0-9]+',x)
print(y)
y = re.findall('AEIOU+',x)
s = 'A message from [email protected] to [email protected] about meeting @2PM'
lst = re.findall('\\S+@\\S+', s)
print(lst)
a = "From [email protected] Sat Jan 5 09:14:16 2008"
# String Parsing Examples
data = "From [email protected] Sat Jan 5 09:14:16 2008"
atpos = data.find('@')
print(atpos)
sppos = data.find(' ',atpos)
print(sppos)
host = data[atpos+1 : sppos]
print(host)
# The Double split pattern
words = data.split()
email = words[1]
print(email)
pieces = email.split('@')
print(pieces[0])
y = re.findall('@([^ ]*)',data)
print(y)
# Escape Character \
x = 'we just received $10.00 for cookies.'
z = re.findall('\$[0-9.]+',x)
print(z)