惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
D
DataBreaches.Net
Recent Announcements
Recent Announcements
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
Y
Y Combinator Blog
博客园 - 【当耐特】
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
P
Proofpoint News Feed
量子位
C
Check Point Blog
F
Fortinet All Blogs
罗磊的独立博客
Last Week in AI
Last Week in AI
GbyAI
GbyAI
L
LangChain Blog
博客园 - 司徒正美

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Python Regex Explained Simply — Extract Anything From Text
Raaga Priya Madhan · 2026-06-12 · via DEV Community
Cover image for Python Regex Explained Simply — Extract Anything From Text

Raaga Priya Madhan

Regex sounds intimidating. It is not. Once you understand the 5 core concepts, you can extract any pattern from any text in seconds. Here is everything you need to know.

What is regex?

Regex is a pattern language. You describe what you are looking for using special characters and Python finds it for you — in any block of text, any size.

Real example: your client sends you a document with 500 customer records mixed with random text. They need all email addresses extracted into Excel. Without regex this takes hours. With regex it takes 3 lines.

import re

text = "Contact john@gmail.com or sales@company.com for details"
emails = re.findall(r'[\w.-]+@[\w.-]+\.\w+', text)
print(emails)
# ['john@gmail.com', 'sales@company.com']

The 5 patterns you need to know

1. \d — any digit

re.findall(r'\d', 'abc123def456')
# ['1', '2', '3', '4', '5', '6']

2. \w — any word character (letter, digit, underscore)

re.findall(r'\w+', 'hello world_123')
# ['hello', 'world_123']

3. + — one or more of the previous

re.findall(r'\d+', 'price is 45000 and tax is 8100')
# ['45000', '8100']

4. [] — any character in this set

re.findall(r'[aeiou]', 'hello world')
# ['e', 'o', 'o']

5. . — any single character

re.findall(r'c.t', 'cat cut cot bat')
# ['cat', 'cut', 'cot']

The 3 functions you will use constantly

re.findall — find all matches

Returns a list of everything that matches the pattern.

text = "Prices: ₹45,000 and ₹12,500 and ₹8,750"
prices = re.findall(r'[\d,]+', text)
print(prices)
# ['45,000', '12,500', '8,750']

re.sub — find and replace

Replaces every match with something else.

messy = "phone: 98-765-43210"
clean = re.sub(r'\D', '', messy)  # remove all non-digits
print(clean)
# '9876543210'

re.search — find first match

Returns just the first match with its position.

text = "Order #A12345 placed successfully"
match = re.search(r'#(\w+)', text)
if match:
    print(match.group(1))  # A12345

A real data cleaning example

Client problem: they have a spreadsheet with phone numbers in 6 different formats. They need them all standardised to 10 digits.

import pandas as pd
import re

df = pd.DataFrame({
    'Phone': ['9876543210', '+91-9876543210', 
              '(080) 4567-8901', '91 98765 43210']
})

def clean_phone(phone):
    digits = re.sub(r'\D', '', phone)
    if len(digits) == 10:
        return digits
    elif len(digits) == 12 and digits.startswith('91'):
        return digits[2:]
    return None

df['Clean'] = df['Phone'].apply(clean_phone)
print(df)

Output:
Phone Clean
0 9876543210 9876543210
1 +91-9876543210 9876543210
2 (080) 4567-8901 None
3 91 98765 43210 9876543210

The one-line summary

Regex is a pattern language — you describe what you are looking for and Python finds every instance of it in any text, any size.

Learn these 5 patterns and 3 functions and you can handle 90% of real data extraction gigs immediately.


Written by Raaga Priya Madhan — CSE student, Bangalore. I build Python automation and data extraction scripts. See my work on GitHub and connect on LinkedIn