What is regular expressions in Python?
Why Interviewers Ask This
This question targets practical, hands-on experience with Python. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.
Answer
Python's re module provides regular expression support. Common functions: re.match(pattern, string) — matches only at the beginning. re.search(pattern, string) — searches anywhere in the string, returns Match object or None. re.findall(pattern, string) — returns list of all non-overlapping matches. re.finditer(pattern, string) — returns iterator of Match objects. re.sub(pattern, repl, string) — replace matches. re.split(pattern, string) — split by pattern. re.compile(pattern) — compile for reuse. Match groups: re.search(r"(\d+)-(\w+)", text).groups(). Flags: re.IGNORECASE, re.MULTILINE, re.DOTALL. Common patterns: \d (digit), \w (word char), \s (whitespace), .* (greedy any), .*? (lazy any). Always use raw strings (r"pattern") to avoid backslash escaping issues.
Common Mistake
Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real Python project.