forked from OldCrow86/DAT6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_re_example.py
More file actions
65 lines (49 loc) · 2.09 KB
/
02_re_example.py
File metadata and controls
65 lines (49 loc) · 2.09 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
This is an intro to regular expressions
I use
https://regex101.com/#python
to check my work!
"""
import re
# flow:
# create a re pattern object
# search (or match) it against text
# orgnize the captures patterns in groups
# \d matches a number
text = "Hello! My name is Sinan. It is 2014 and it's amazing."
pattern1 = re.compile("\d")
re.search(pattern1, text) # == a search object
# use group to get each instance in the regular expression
# \d is just ONE number, so it only finds the "2" in "2014"
re.search(pattern1, text).group(0)
# adding a + means "at least one" but potentially more
pattern2 = re.compile("\d+")
re.search(pattern2, text).group(0) # == '2014'
# use square brackets [] to match one of the items present
alphabet = 'abcfgfcgfgccffgfgcfcgfgfIBETITENDSHEREfg'
pattern3 = re.compile('cgf')
re.search(pattern3, alphabet).group(0)
phone_pattern = re.compile("\d\d\d-\d\d\d-\d\d\d\d")
# this will capture something like a phone pattern
re.search(phone_pattern, "my phone number is 609-4699992-6706 dude").group(0)
# . matches ANYTHING
all_of_the_text = "dmzhvbekuhvbc dfljghwco87rc6geinsr6t4gi7rgwefiuvbekuhvbdfljghwco87rc6geinsr6t4gi7rgwefiu ywgsfybcstzvgbrtybte"
anything_pattern = re.compile(".+")
re.search(anything_pattern, all_of_the_text).group(0)
# \w matches any word character, alphanumeric
more_text = "To be or not to be in 2015"
text_pattern = re.compile('\w*')
re.search(text_pattern, more_text).group(0)
# notice how it stopped at the space, because a space is NOT
# alphanumeric!
sentence = "I am happy to be here"
# will match exactly "am" and then anything, and then "to"
complicated_pattern = re.compile("am .* to")
re.search(complicated_pattern, sentence).group(0)
# if you want to match an actual period, do \.
# try making an email pattern that will take in any numberof alphanumeric
# characters and periods
# accompanied with an @ and the ending pattern we will assume is just .com
email_pattern = re.compile("[\w\.]*@[\w\.]+\.com")
email_re = re.search(email_pattern, "my email address is sinan.u.ozdemir@gmail.com")
email_re.group(0) # == 'sinan.u.ozdemir@gmail.com'