regex Flashcards
(33 cards)
1
Q
Digit
regex
2 alternatives
A
\d
2
Q
a or b
regex
A
(a|b)
3
Q
Group
regex
A
(...)
4
Q
Range (a or b or c)
regex
A
[abc]
5
Q
Not (a or b or c)
A
[^abc]
6
Q
Lower case letter from a to q
A
[a-q]
7
Q
Upper case letter from A to Q
A
[A-Q]
8
Q
Digit from 0 to 7
A
[0-7]
9
Q
Look-ahead assertions
A
?=
10
Q
Negative look-ahead
A
?!
11
Q
Look-behind assertion
A
?<=
12
Q
Negative look-behind
A
?!=
?<!
13
Q
Any character except new line
A
.
14
Q
0 or more
A
*
15
Q
1 or more
A
+
16
Q
Exactly 3
A
{3}
17
Q
3 or more
A
{3,}
18
Q
3, 4, or 5
A
{3-5}
19
Q
Not digit
A
\D
20
Q
Word
A
\w
21
Q
Not word
A
\W
22
Q
White space
A
\s
23
Q
Not white space
A
\S
24
Q
Start of string or line
A
25
Start of string
\A
26
End of string or line
\$
27
End of string
\Z
28
Word boundary
\b
29
Not word boundary
\B
30
Octal character xxx
\xxx
31
Hex character hh
\xhh
32
Use regular expressions
import re
matches = re.findall(r'\b\d+\b', text)
33
Cleaning adresses
import re
def clean_address(address):
pattern = r'\d+\s+(?:[A-Z][a-z]+\s+)+' # Match street address part
match = re.search(pattern, address)
if match:
return match.group(0).title() # Return it capitalized
else:
return address # Leave it as is if no match