Regular Expression Flashcards
(32 cards)
1
Q
any character
A
.
2
Q
zero or more
A
*
3
Q
one or more
A
+
4
Q
whitespace (space, newline, tab)
A
\s
5
Q
two characters
A
\w{2}
6
Q
search for match
A
re. search(pattern, string) # at most one match
re. findall(pattern, string) # list of matches
7
Q
get first tuple from re.search
A
re.search(pattern, string).group(1) # 0 is full match
8
Q
beginning of line or string
A
9
Q
end of line or string
A
$
10
Q
all uppercase letters
A
[A-Z]
11
Q
a, b or c
A
[abc]
12
Q
not b
A
[^b]
13
Q
not lowercase letters
A
[^a-z]
14
Q
neither n or o
A
[^no]
15
Q
this or that
A
(this|that)
16
Q
any word character
A
\w
[a-zA-Z0-9_]
17
Q
any digit
A
\d
18
Q
any non-word character
A
\W
19
Q
any non-digit
A
\D
20
Q
any non-whitespace character
A
\S
21
Q
word boundary
A
\b (not inside [])
22
Q
no word boundary
A
\B
23
Q
special characters that need quoting
A
. | [ ( * ^ / { ?
24
Q
optional minus-sign
A
-?
25
non-greedy .*
.*?
26
name a group
(?P\d)
| match.group('number')
27
split string
re.split(r"\s+", str)
28
ignore case
re.search(pattern, string, re.IGNORECASE)
29
re.MULTILINE
^ and $ matches beginning and end of line
30
let . match newline characters
re.DOTALL
31
avoid the need to quote backslashes
raw string: r"..."
32
replace
re.sub(this, withThis, str)