_CORE
AI & Agentic Systems Core Information Systems Cloud & Platform Engineering Data Platform & Integration Security & Compliance QA, Testing & Observability IoT, Automation & Robotics Mobile & Digital Banking & Finance Insurance Public Administration Defense & Security Healthcare Energy & Utilities Telco & Media Manufacturing Logistics & E-commerce Retail & Loyalty
References Technologies Blog Know-how Tools
About Collaboration Careers
CS EN
Let's talk

Regex: Cheat Sheet with Practical Examples

11. 10. 2024 1 min read intermediate

Regular expressions are a powerful tool — if you know how. Practical cheat sheet to bookmark.

Basic Characters

. \d \D \w \W \s \S \b

Quantifiers

* + ? {3} {2,5} {3,} *?

Groups

(abc) (?:abc) a|b \1

Lookahead/behind

(?=x) (?!x) (?<=x) (?<!x)

Examples

Email: [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}
IP: \b(?:\d{1,3}.){3}\d{1,3}\b
Date: \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Phone CZ: (?:+420)?\s?\d{3}\s?\d{3}\s?\d{3}

In Practice

grep -oP ‘\b\d{1,3}(.\d{1,3}){3}\b’ access.log
import re; emails = re.findall(r’[\w.+-]+@[\w-]+.[\w.]+’, text)

Tip

Test on regex101.com. And if regex exceeds 2 lines, consider a parser.

regexprogrammingreference
Share:

CORE SYSTEMS tým

Stavíme core systémy a AI agenty, které drží provoz. 15 let zkušeností s enterprise IT.