-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path18_regexes03_specialchar.py
69 lines (56 loc) · 1.6 KB
/
18_regexes03_specialchar.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
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
66
67
68
69
# Regexes are useful because we can find patterns of text
# using certain special characters
import re
# \d
# matches a number
# Note that re.match() only finds something at the beginning
# of the string, so we will use "search" instead
result = re.search(r'\d', "It is the year 2018.")
print(result)
# \D
# matches not a number
result = re.search(r'\D', "It is the year 2018.")
print(result)
# \s
# matches whitespace
result = re.search(r'\s', "It is the year 2018.")
print(result)
# \S
# matches NOT whitespace
result = re.search(r'\S', "It is the year 2018.")
print(result)
# There are other whitespace special characters:
# \t matches a tab, \n matches a new line
# \w
# matches letters and numbers
result = re.search(r'\w', "It is the year 2018.")
print(result)
# \W
# matches NOT letters and numbers
result = re.search('r\W', "It is the year 2018.")
print(result)
# .
# matches anything except a new line
result = re.search(r'.', "It is the year 2018.")
print(result)
# \b
# matches a word boundary (so we won't find the ship in
# "friendship"):
result = re.search(r"\bship","Will we find friendship or an ocean ship?")
print(result)
# ?
# make optional:
result = re.search(r"humou?r","Americans spell it humor.")
print(result)
# +
# Used in conjunction with something else, matches one or more
# instances of the pervious term:
result = re.search(r'\d+', "It is the year 2018.")
print(result)
# *
# Used in conjunction with something else, matches zero or
# more instances of the previous term:
result = re.search(r'humou*r', "humor.")
print(result)
result = re.search(r'humou*r', "humouuuuuuuuuur.")
print(result)