Regular Expressions in Python — Python Technical Interview Questions & Answers

Regular Expressions in Python

Regular Expressions in Python

  • Regex finds text patterns. import re. re.search looks anywhere. re.match looks at the start. re.findall returns all matches. re.sub replaces. \d is a digit. \w is a word character. Use a raw string r"\d{10}" for a 10-digit mobile so backslashes stay sane.
  • Fresher example: re.findall(r"\d+", "Asha 90 Ravi 85") → ['90', '85']. Don’t parse a mobile with ten if-checks when one pattern works. search vs match: match only at the beginning.
  • Regular Expressions in Python — output: True then True. 10 digits; Asha letters only.

Technical Interview Tip: re.search vs re.match if asked.

Code Examples for Regular Expressions in Python

Example

# Regex
import re
print(bool(re.fullmatch(r"\d{10}", "9876543210")))
print(bool(re.fullmatch(r"[A-Za-z]+", "Asha")))

Regular Expressions in Python — output: True then True. 10 digits; Asha letters only.

How to Explain Regular Expressions in Python to an Interviewer

Explain Regular Expressions as if you are teaching a junior — definition, then one tiny script.

import re. Pattern match. Use r"..." raw strings.

What does the example print, and why?

Regular Expressions in Python — output: True then True

What mistake do freshers make with Regular Expressions?

search vs match.