Python RegEx

Python · Working with Data

Theory

A regular expression (regex) is a mini-language for describing text patterns, and Python's re module applies them. \d means a digit, \w a word character, \s whitespace, . any character; + means one or more, * zero or more, {3} exactly three; [A-Z] is a character class and ( ) creates a capture group. Writing patterns as raw strings, r'\d+', stops Python from treating backslashes as escapes.

The main functions: re.search() finds the first match anywhere and returns a match object (or None); re.match() only checks the start of the string; re.findall() returns every match as a list; re.sub() replaces matches. re.findall(r'\d+', 'Order 123, Item 45') returns ['123', '45'] - note they are strings, so convert with int() if needed.

Why we use it

Text data is messy: phone numbers, order IDs, emails and codes hide inside free text. Regex extracts, validates or cleans them in one line where manual string slicing would take many.

When to use it

Use regex for pattern-based tasks such as extracting numbers, validating formats or cleaning text. For simple fixed checks, string methods like startswith(), split() or 'in' are clearer and faster.

How it works

  1. Write a pattern, e.g. r'\d+'
  2. Scan the text from left to right
  3. Does the text here match? Yes: Record it and continue after it. No: Move one character forward.
  4. Repeat until the end of the text
  5. findall returns ['123', '45']

Syntax

import re
re.match(pattern, text)

Example

re.findall(r'\d+', 'Order 123, Item 45')
# ['123', '45']

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic