What is the primary function in the re module to compile a regex pattern?
re.compile(pattern, flags=0)
What is the main reason to compile a regular expression?
Efficiency. If you use the same pattern multiple times, compiling it once is faster than having Python re-interpret the pattern string every time.
What does the re.compile() function return?
A RegexObject (also called a Pattern object).
How do you import the re module?
import re
Example: How to compile a simple pattern to find digits?
digit_pattern = re.compile(r'\d+')
Once you have a compiled pattern object, how do you search for a match in a string?
Use the object’s .search() method: match = digit_pattern.search(my_string)
How does pattern.search(string) differ from re.search(pattern, string)?
Functionally, they are similar, but pattern.search(string) is slightly faster because the pattern is already compiled.
How do you find all matches in a string using a compiled pattern?
Use the object’s .findall() method: all_matches = digit_pattern.findall(my_string)
How do you perform a substitution using a compiled pattern?
Use the object’s .sub() method: new_string = digit_pattern.sub('REPLACED', my_string)
What are flags in re.compile()?
Optional arguments that modify the behavior of the regular expression (e.g., making it case-insensitive).
How do you make a compiled pattern case-insensitive?
Pass the re.IGNORECASE flag: pattern = re.compile(r'hello', re.IGNORECASE) (or re.I)
What does the re.MULTILINE flag (or re.M) do?
It makes ^ (start) and $ (end) match the beginning and end of each line, not just the beginning and end of the entire string.
What does the re.DOTALL flag (or re.S) do?
It makes the . (dot) metacharacter match every character, including the newline character (which it normally does not).
How do you pass multiple flags to re.compile()?
Combine them using the bitwise OR operator (|): flags = re.IGNORECASE | re.MULTILINE\npattern = re.compile(r'^hello', flags)
Why is it highly recommended to use raw strings (e.g., r'mypattern') when defining regex patterns?
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.