Regular Expressions Flashcards

1
Q

What module function is used to find a regex pattern to match?

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

A function used to find the first location of regex pattern.

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

How to display the matched regex pattern?

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

If you want to separate the regex pattern into different objects. What would you use?

A

Use parentheses.

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

A function used to print the first object of a regex pattern.

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

A function used to print multiple regex objects in a tuple.

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

This technique assigns the two values in the tuple into the two variables.

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

Code for finding the first instance of a phone number split it into 2 different objects

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

What do you need to match any of these special characters?
. ^ $ * + ? { } [ ] \ | ( )

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

If we want to search for the first group of a regex object. If not found move to the 2nd group, we would use what?

A

The pipe (|)

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

Code a regex object that looks for the string “Batman” as the first choice. If not found, then search for the string “Tina Fey”.

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

What character would you use to make a group an optional part of the search pattern?

A

The question mark (?)

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

Code a regex pattern that makes the string “wo” optional in the word “Batman”?

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

What character would you use to match a group ZERO or more times?

A

Star or asterisk (*)

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

Code a regex pattern that allows us to match the “wo” string inside of “Batman” zero or more times.

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

What character would you use to match a group ONE or more times?

A

Plus sign (+)

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

Code a regex pattern where the string “wo” inside of “Batman” has to be found at least once.

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

Code that specifies the group (Ha) should be matched exactly 3 times.

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

What character would you use to specify how many times a group should appear for it to be a match?

A

Brackets {}

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

Code that specifies the group (Ha) should be matched between 3 to 5 times.

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

Code that specifies the group (Ha) should be matched 3 times and beyond.

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

By default, Python regular expressions are greedy. What does this mean?

A

They match the longest string possible.

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

Given the regex pattern r”(Ha){3,5}” Python will search for what number?

A

5, because it’s greedy by default and this is the highest number in the range.

24
Q

Given the regex pattern r”(Ha){3,5}?” Python will search for what number?

A

3, the question mark makes this expression non-greedy. So, it matches with the shortest number in the range.

25
What character do we use to make a range non-greedy?
Question mark (?)
26
What method do we use to match all regular expression objects?
.findall()
27
Code that matches the regex pattern for all phone numbers in a text and places them in a list.
28
Code that matches the regex pattern for all phone numbers, digits broken into 3 groups. Resulting in the matched groups placed into a list of tuples.
29
Shorthand character class that represents any numeric digit from 0 to 9.
\d
30
Shorthand character class that represents any character that is NOT a numeric digit from 0 to 9.
\D
31
Shorthand character class that represents any letter, numeric digit, or underscore character. (Think matching ”word” characters)
\w
32
Shorthand character class that represents any character NOT a letter, numeric digit, or underscore.
\W
33
Shorthand character class that represents any space, tab, or newline character. (Think matching space)
\s
34
Shorthand character class that represents any character NOT a space, tab, or newline.
\S
35
What can you do to match a set of specific characters? And shorthand characters are too broad?
Create character class.
36
What’s a character class for matching all upper/lower letters and numbers?
37
What’s a character class for matching all vowels (upper/lower)?
38
What’s a negative character class? And it’s symbol?
We match all characters NOT in a character class. Represented by a caret (^) in front of the character class.
39
What’s a character class for matching all characters NOT vowel characters (upper/lower)?
40
What character do you use to indicate that a match must occur at the beginning of a regex pattern?
Caret (^)
41
What character do you use to indicate that a match must occur at the end of a regex pattern?
Dollar sign ($)
42
Code for finding a word that starts with Hello.
43
Code for finding a word that ends with a number.
44
What character do you use to match any one character? (except for Newline)
Dot (.)
45
Code for finding a word that ends in “at” and with one character in front.
46
How would you make the dot-star which by default is greedy, non-greedy?
Question mark (?) ie .*?
47
What character do you use to match any character (except for newline)?
The dot-star (.*)
48
Code for matching any group after the First and Last name.
49
Code for matching the first group object after the First and Last name (non-greedy).
50
How would one match everything with the dot character including the newlines?
51
How would you match a word regardless of whether it’s uppercase/lowercase letters?
52
The method used for finding a regex pattern and substituting it with another one.
sub()
53
Write code substituting any word after “Agent” with the word “CENSORED”.
54
Write code censoring the name of the Agents by showing their first initials.
55
What command would we use to spread regular expression patterns over multiple lines with comments inside of the compile() function?
re.VERBOSE
56
What’s the workaround for using multiple commands at the same time inside of the compile() function?
The pipe (|)