Regular expressions (regex) are powerful tools for pattern matching and text manipulation. Here's a quick guide to get started:
📌 Basic Concepts
- Literal Characters: Match exact characters (e.g.,
a
matches "a") - Special Characters: Use
.
to match any single character,*
for zero or more repetitions - Character Classes:
[abc]
matches any one of "a", "b", or "c" - Anchors:
^
asserts the start of a line,$
asserts the end
🔍 Common Use Cases
- Validation: Check email formats (
^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$
) - Search & Replace: Replace all digits with
#
using\d
in replacement - Splitting Text: Divide strings by whitespace with
\s+
- Extracting Data: Capture group matches using parentheses
()
✅ Example Code
import re
text = "The price is $123.45"
match = re.search(r'\$(\d+\.?\d*)', text)
print(match.group(1)) # Output: 123.45
🚀 Advanced Tips
- Lookaheads: Use
(?=...)
to assert a subpattern without consuming characters - Escaping: Escape special characters with
\
(e.g.,\.
matches a literal dot) - Flags: Add
re.IGNORECASE
for case-insensitive matching
For deeper exploration, check our Regex Two guide on complex patterns and performance optimization.