Regular Expression Flashcards

1
Q

any character

A

.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

zero or more

A

*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

one or more

A

+

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

whitespace (space, newline, tab)

A

\s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

two characters

A

\w{2}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

search for match

A

re. search(pattern, string) # at most one match

re. findall(pattern, string) # list of matches

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

get first tuple from re.search

A

re.search(pattern, string).group(1) # 0 is full match

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

beginning of line or string

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

end of line or string

A

$

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

all uppercase letters

A

[A-Z]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

a, b or c

A

[abc]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

not b

A

[^b]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

not lowercase letters

A

[^a-z]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

neither n or o

A

[^no]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

this or that

A

(this|that)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
Q

non-greedy .*

A

.*?

26
Q

name a group

A

(?P\d)

match.group(‘number’)

27
Q

split string

A

re.split(r”\s+”, str)

28
Q

ignore case

A

re.search(pattern, string, re.IGNORECASE)

29
Q

re.MULTILINE

A

^ and $ matches beginning and end of line

30
Q

let . match newline characters

A

re.DOTALL

31
Q

avoid the need to quote backslashes

A

raw string: r”…”

32
Q

replace

A

re.sub(this, withThis, str)