Fundamentals 5 min read

Master Python Regex: Essential re Module Functions and Patterns Explained

This article revisits Python’s re module, covering functions like re.search, re.match, and re.sub, explaining common regex symbols, quantifiers, character classes, grouping, and findall usage, with clear examples and illustrations to help beginners master regular expression patterns.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Regex: Essential re Module Functions and Patterns Explained

While writing a crawler, I recalled a great article I read when first learning regular expressions and found it again.

re Module

re.search

We often use match = re.search(pat, str). Since it may not find a match, re.search() is usually followed by an if statement.

re.match

re.match

is similar to re.search, but it matches only from the beginning of the string.

Common Regex Symbols

a, X, 9 : match themselves; metacharacters like . ^ $ * + ? { } [ ] \ | ( ) have special meanings.

. : matches any character except '\n'.

\w : matches word characters [a-zA-Z0-9_].

\W : matches non‑word characters.

\b : matches a word boundary.

\s : matches a whitespace character (space, newline, tab, etc.).

\S : matches a non‑whitespace character.

\t, \n, \r : match tab, newline, carriage return.

\d : matches digits [0-9].

^ : matches the start of a string.

$ : matches the end of a string.

Quantifiers

‘+’ means one or more times, ‘*’ means zero or more times, ‘?’ means zero or one time.

Character Classes []

Square brackets indicate a set of characters, so [abc] matches ‘a’ or ‘b’ or ‘c’.

Group Extraction with ()

Parentheses can capture parts of a match. For example, the pattern r'([\w.-]+)@([\w.-]+)' can extract the username and hostname of an email; after a successful match, match.group(1) and match.group(2) return the captured parts while match.group() returns the whole match.

findall and Groups

Combining () with findall() returns a list of tuples when one or more groups are present.

Adding ^ to re.search makes it behave like re.match.

re.sub

re.sub(pat, replacement, str)

searches for substrings matching pat in str and replaces them with replacement. The replacement can include \1 or \2 to refer to captured groups, enabling partial substitution.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

regular expressionsregexpattern-matchingre module
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.