Fundamentals 6 min read

Master Python Regex: 14 Essential Special Characters Explained

This article provides a concise tutorial on Python regular expression special characters, explaining the meaning and usage of symbols such as \d, \w, ^, $, *, +, ?, {n}, {n,}, {n,m}, |, [], and others, accompanied by illustrative examples and images.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Python Regex: 14 Essential Special Characters Explained

Today we share the last special character of regular expressions, \d, with a tutorial.

The special character \d represents a digit.

To match the specific string 2004 , add the non‑greedy quantifier ? . This switches to non‑greedy mode, matching the shortest possible sequence and producing 2004 as output.

Curly braces specify the exact length of a numeric match. For example, {4} matches exactly four consecutive digits.

The article then reviews fourteen common regex symbols:

^ anchors the match to the start of a string.

* allows the preceding element to appear zero or more times.

. matches any single character.

$ anchors the match to the end of a string.

? makes the preceding quantifier non‑greedy.

+ requires the preceding element to appear one or more times.

{2} , {2,} , {2,5} specify exact, minimum, or range repetitions respectively.

| acts as a logical OR between alternatives.

[] , [A-Za-z0-9] , [^] define character classes, ranges, and negations.

\s matches whitespace, while \S matches non‑whitespace.

\w matches alphanumeric characters and underscore (equivalent to [A-Za-z0-9_] ); \W matches any character not in that set.

[\u4E00-\u9FA5] matches Chinese characters.

() creates capturing groups for extracting substrings.

\d matches digits (0‑9).

Each symbol is illustrated with example images throughout the article.

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.

PythonTutorialregexSpecial Characters
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

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.