Regular Expression (Python) Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Importing the regular expression module

A

import re

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

Use a function to search for ‘blue’ ins string ‘Rythm and blues’

A

re.search(r”blue”, “Rhythm and blues”)

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

Regex to match the following in order:
A single open bracket character.
One or more word characters.
A single close bracket character.

A

[/w+]

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

Any character except f , F, d, 3, or whitespace

A

[^fFud\s]

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

Non-digit characters

A

\D

  --OR--

[^0-9]

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

Any three characters except newline then followed by a period

A

.{3}.

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

Find ‘th’ only when not at the start or end of a word

A

\Bth\B

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

find ‘abc’ only when at the start or end of a string

A

^abc|abc$

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

Regex to match the following in order:
Capture group of one non-word character then digit
Non-whitespace then ‘'
The capture group repeated 2 to 4 times

A

(\W\d)\s\\1{2,4}

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

Create a non-capture group containing ‘kevin’ or ‘candace’

A

(?:kevin|candace)

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

Any letter repeated at least twice

A

([a-zA-Z])\1{1,}

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

Matches ‘zzz’ only when it is followed by ‘abc’

A

zzz(?=abc)

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

Matches ‘zzz’ only when it is not followed by ‘abc’

A

zzz(?!abc)

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

Matches ‘zzz’ only when it is preceded by ‘abc’

A

(?<=abc)zzz

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

Matches ‘zzz’ only when it is not preceded by ‘abc’

A

(?

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

What string does this regex match?

(yes)(no)\2\1

A

yesnonoyes

17
Q

Create a raw string ‘\tTab’ in Python so that the backslash is interpreted literally

A

r”\tTab”