Regular expression
Absolutely! Let’s break down Python regex in a clear and structured way. I’ll explain from the basics to practical usage.
1️⃣ What is Regex?
Regex (Regular Expression) is a pattern that describes a set of strings.
It’s used to search, match, replace, or validate text.
In Python, regex is handled using the built-in
remodule.
2️⃣ Basic re Module Functions
re Module Functionsimport rere.match(pattern, string)
Checks if the start of the string matches the pattern.
re.search(pattern, string)
Searches for first occurrence of pattern anywhere in string.
re.findall(pattern, string)
Returns a list of all matches.
re.finditer(pattern, string)
Returns an iterator of match objects.
re.sub(pattern, repl, string)
Replace matches with repl.
re.split(pattern, string)
Split string by the pattern.
3️⃣ Basic Patterns
.
Any character except newline
a.c matches abc, a1c
\d
Digit (0-9)
\d matches 5
\D
Non-digit
\D matches a
\w
Word character (a-z, A-Z, 0-9, _)
\w+ matches hello_123
\W
Non-word character
\W matches !
\s
Whitespace
\s matches space, tab, newline
\S
Non-whitespace
\S matches a
^
Start of string
^Hello matches Hello world
$
End of string
world$ matches Hello world
[...]
Character set
[abc] matches a or b or c
[^...]
Negated set
[^abc] matches anything except a, b, c
*
0 or more repetitions
a* matches '', a, aa
+
1 or more repetitions
a+ matches a, aa
?
0 or 1 repetition
a? matches '' or a
{n}
Exactly n repetitions
a{3} matches aaa
{n,}
n or more repetitions
a{2,} matches aa, aaa, aaaa
{n,m}
Between n and m repetitions
a{2,4} matches aa, aaa, aaaa
()
Capture group
(ab)+ matches ab, abab
4️⃣ Examples
Match digits in a string
Validate email
Replace all spaces with _
_Extract domain from URL
5️⃣ Tips
Always use raw strings (
r"pattern") to avoid issues with backslashes.Test regex on websites like regex101.com before using in code.
Use groups
()to extract parts of a match.re.compile(pattern)can be used to compile a regex for repeated use (faster).
If you want, I can make a Python regex cheatsheet with examples for all commonly used patterns and functions—it’ll be super handy to keep.
Do you want me to do that?
Last updated