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
Q

What character do we use to make a range non-greedy?

A

Question mark (?)

26
Q

What method do we use to match all regular expression objects?

A

.findall()

27
Q

Code that matches the regex pattern for all phone numbers in a text and places them in a list.

A
28
Q

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.

A
29
Q

Shorthand character class that represents any numeric digit from 0 to 9.

A

\d

30
Q

Shorthand character class that represents any character that is NOT a numeric digit from 0 to 9.

A

\D

31
Q

Shorthand character class that represents any letter, numeric digit, or underscore character. (Think matching ”word” characters)

A

\w

32
Q

Shorthand character class that represents any character NOT a letter, numeric digit, or underscore.

A

\W

33
Q

Shorthand character class that represents any space, tab, or newline character. (Think matching space)

A

\s

34
Q

Shorthand character class that represents any character NOT a space, tab, or newline.

A

\S

35
Q

What can you do to match a set of specific characters? And shorthand characters are too broad?

A

Create character class.

36
Q

What’s a character class for matching all upper/lower letters and numbers?

A
37
Q

What’s a character class for matching all vowels (upper/lower)?

A
38
Q

What’s a negative character class? And it’s symbol?

A

We match all characters NOT in a character class.
Represented by a caret (^) in front of the character class.

39
Q

What’s a character class for matching all characters NOT vowel characters (upper/lower)?

A
40
Q

What character do you use to indicate that a match must occur at the beginning of a regex pattern?

A

Caret (^)

41
Q

What character do you use to indicate that a match must occur at the end of a regex pattern?

A

Dollar sign ($)

42
Q

Code for finding a word that starts with Hello.

A
43
Q

Code for finding a word that ends with a number.

A
44
Q

What character do you use to match any one character? (except for Newline)

A

Dot (.)

45
Q

Code for finding a word that ends in “at” and with one character in front.

A
46
Q

How would you make the dot-star which by default is greedy, non-greedy?

A

Question mark (?) ie .*?

47
Q

What character do you use to match any character (except for newline)?

A

The dot-star (.*)

48
Q

Code for matching any group after the First and Last name.

A
49
Q

Code for matching the first group object after the First and Last name (non-greedy).

A
50
Q

How would one match everything with the dot character including the newlines?

A
51
Q

How would you match a word regardless of whether it’s uppercase/lowercase letters?

A
52
Q

The method used for finding a regex pattern and substituting it with another one.

A

sub()

53
Q

Write code substituting any word after “Agent” with the word “CENSORED”.

A
54
Q

Write code censoring the name of the Agents by showing their first initials.

A
55
Q

What command would we use to spread regular expression patterns over multiple lines with comments inside of the compile() function?

A

re.VERBOSE

56
Q

What’s the workaround for using multiple commands at the same time inside of the compile() function?

A

The pipe (|)