In short
Extracting phone numbers from messy text, validating a PIN code or cleaning up a CSV column: regular expressions describe text patterns in a compact way, and Python's re module puts them to work.
Regular expressions describe text patterns, and the re module uses them to search, extract, validate and replace text, such as finding all email addresses or checking a PIN code format.

Think of it like this
A metal detector on the beach
You don't dig up the whole beach looking for coins. You sweep a detector tuned to 'metal, this size', and it beeps only where something matches. A regex is that tuning: 'six digits', 'something@something.com'. re.findall sweeps the text and digs up every match.
Watch it happen
How re.search(r'\d{6}', text) finds a PIN code
1/4Text: 'Ship to Flat 12, Jaipur 302017, Rajasthan'.
The big ideas
What you need to know
Building blocks
\d is a digit, \w a word character, \s whitespace, . any character. {6} means exactly six, + one or more, * zero or more, ? optional.
Use raw strings
Write patterns as r'\d+' so Python doesn't treat backslashes as its own escapes.
The main functions
search finds the first match anywhere, fullmatch checks the whole string, findall returns all matches, sub replaces them.
Groups
Brackets capture parts: (\d{2})-(\d{2})-(\d{4}) lets you pull out day, month and year separately.
See it in code
Walk through the code
1import re2 3msg = "Call Ravi at 98765 43210 or 9123456789. Mail: ravi.k@example.in"4 5phones = re.findall(r"[6-9]\d{4}\s?\d{5}", msg)6print(phones) # ['98765 43210', '9123456789']7 8email = re.search(r"[\w.+-]+@[\w-]+\.[\w.]+", msg)9if email:10 print(email.group()) # ravi.k@example.in11 12def is_valid_pin(pin: str) -> bool:13 return re.fullmatch(r"[1-9]\d{5}", pin) is not None14print(is_valid_pin("560001"), is_valid_pin("056001")) # True False15 16date_pattern = re.compile(r"(\d{2})-(\d{2})-(\d{4})")17m = date_pattern.search("Invoice dated 15-08-2026")18if m:19 day, month, year = m.groups()20 print(f"{year}/{month}/{day}") # 2026/08/1521 22clean = re.sub(r"\s+", " ", "too many spaces")23print(clean) # too many spaces1/5Mobile numbers start with 6 to 9, then nine more digits with an optional space after the fifth. findall returns every match.
Pulling Indian mobile numbers and an email out of text, validating a PIN code, capturing date parts and cleaning spaces.
Save yourself the pain
Common mistakes
Oops
Writing patterns as normal strings like '\d+' and getting warnings or odd results.
Do this instead
Always use raw strings: r'\d+'.
Oops
Using re.search to validate a whole input, so 'abc560001xyz' passes as a PIN.
Do this instead
Use re.fullmatch, or anchor the pattern with ^ and $.
Oops
Using regex to parse HTML or JSON.
Do this instead
Use a proper parser (BeautifulSoup, json). Regex is for simple, flat text patterns.
Your mission
Clean a contact list
Quick check
Which function checks that the entire string matches the pattern?
Go deeper
- 1Extract all 10-digit mobile numbers from a block of text with re.findall.
- 2Validate a 6-digit Indian PIN code with re.fullmatch.
- 3Always write patterns as raw strings like r'\d+' and test them on regex101.com.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.