forked from rasbt/python_reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreg_expr_2_operators.py
More file actions
127 lines (105 loc) · 3.13 KB
/
reg_expr_2_operators.py
File metadata and controls
127 lines (105 loc) · 3.13 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Examples for using Python's Regular expression module "re"
# sr 11/30/2013
import re
'''OVERVIEW
'*' matches all characters that follow (0 or more)
'+' matches all characters that follow (1 or more)
'?' makes the previous character optional
'{4}' previous character must match exactly 4 times
'{2-4}' previous character must match exactly 2-4 times
'[0-9]' matches all characters in the set of numbers 0 to 9
'[A-Z]' matches all characters in the set of A to Z
'\d' matches all digits, e.g., '4', '9' ...
'\D' matches all NON-digit characters
'\s' matches all space characters: '', '\t', '\r', '\n'
'\S' matches all NON-space characters
'\w' matches all non-punctuation characters (i.e., letters and digits)
'\W' matches all NON-letter and NON-digit characters
'^bla' NOT-matches 'bla'
'let$' matches 'let' but not 'letter'
'\b' matches transition between non-word characters and word characters
'''
data = '''2013-01-01
2012-02-02
aaaa-02-02
aa-02-02
-04-04
2000 02-02
ghi stu
2012-03-03'''.strip().split('\n')
# A >> '*' matches all characters that follow (0 or more)
print (50*'-' + '\nA\n' + 50*'-')
for line in data:
match = re.search('(.*)-(..)-(..)', line) # note the parantheses
if match:
print(match.group(1), match.group(2), match.group(3))
'''
--------------------------------------------------
A
--------------------------------------------------
2013 01 01
2012 02 02
aaaa 02 02
aa 02 02
04 04
2012 03 03
'''
# B >> '+' matches all characters that follow (1 or more)
print (50*'-' + '\nB\n' + 50*'-')
for line in data:
match = re.search('(.+)-(..)-(..)', line) # note the parantheses
if match:
print(match.group(1), match.group(2), match.group(3))
'''
--------------------------------------------------
B
--------------------------------------------------
2013 01 01
2012 02 02
aaaa 02 02
aa 02 02
2012 03 03
'''
# C >> '?' makes the previous character optional
print (50*'-' + '\nC\n' + 50*'-')
for line in data:
match = re.search('(.+)-?(..)-(..)', line) # note the parantheses
if match:
print(match.group(1), match.group(2), match.group(3))
'''
--------------------------------------------------
C
--------------------------------------------------
2013- 01 01
2012- 02 02
aaaa- 02 02
aa- 02 02
- 04 04
2000 02 02
2012- 03 03
'''
# D >> '{4}' previous character must match exactly 4 times
print (50*'-' + '\nD\n' + 50*'-')
for line in data:
match = re.search('(a{4})-(..)-(..)', line) # note the parantheses
if match:
print(match.group(1), match.group(2), match.group(3))
'''
--------------------------------------------------
D
--------------------------------------------------
aaaa 02 02
'''
# E >>'{2-4}' previous character must match exactly 2-4 times
print (50*'-' + '\nE\n' + 50*'-')
for line in data:
match = re.search('(a{2,4})-(..)-(..)', line) # note the parantheses
if match:
print(match.group(1), match.group(2), match.group(3))
'''
--------------------------------------------------
E
--------------------------------------------------
aaaa 02 02
aa 02 02
'''