How do you compile a regular expression in python? (AI) Flashcards

(15 cards)

1
Q

What is the primary function in the re module to compile a regex pattern?

A

re.compile(pattern, flags=0)

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

What is the main reason to compile a regular expression?

A

Efficiency. If you use the same pattern multiple times, compiling it once is faster than having Python re-interpret the pattern string every time.

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

What does the re.compile() function return?

A

A RegexObject (also called a Pattern object).

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

How do you import the re module?

A

import re

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

Example: How to compile a simple pattern to find digits?

A

digit_pattern = re.compile(r'\d+')

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

Once you have a compiled pattern object, how do you search for a match in a string?

A

Use the object’s .search() method: match = digit_pattern.search(my_string)

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

How does pattern.search(string) differ from re.search(pattern, string)?

A

Functionally, they are similar, but pattern.search(string) is slightly faster because the pattern is already compiled.

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

How do you find all matches in a string using a compiled pattern?

A

Use the object’s .findall() method: all_matches = digit_pattern.findall(my_string)

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

How do you perform a substitution using a compiled pattern?

A

Use the object’s .sub() method: new_string = digit_pattern.sub('REPLACED', my_string)

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

What are flags in re.compile()?

A

Optional arguments that modify the behavior of the regular expression (e.g., making it case-insensitive).

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

How do you make a compiled pattern case-insensitive?

A

Pass the re.IGNORECASE flag: pattern = re.compile(r'hello', re.IGNORECASE) (or re.I)

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

What does the re.MULTILINE flag (or re.M) do?

A

It makes ^ (start) and $ (end) match the beginning and end of each line, not just the beginning and end of the entire string.

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

What does the re.DOTALL flag (or re.S) do?

A

It makes the . (dot) metacharacter match every character, including the newline character (which it normally does not).

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

How do you pass multiple flags to re.compile()?

A

Combine them using the bitwise OR operator (|): flags = re.IGNORECASE | re.MULTILINE\npattern = re.compile(r'^hello', flags)

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

Why is it highly recommended to use raw strings (e.g., r'mypattern') when defining regex patterns?

A

Raw strings prevent backslashes (\) from being interpreted as Python escape sequences, so r'\n' is a literal backslash and ‘n’, not a newline. This is crucial for regex patterns like \d or \w.

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