Fundamentals 5 min read

Master Python Regex: 14 Essential Special Characters Explained

This article provides a concise tutorial on Python regular expression special characters, detailing the meaning and usage of symbols such as \d, ?, +, {n}, |, and others, illustrated with examples and images, helping readers quickly grasp how to match digits, whitespace, word characters, and more in pattern matching.

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

Python Regular Expression Special Characters Overview

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

1. \d – digit

The \d character matches any digit. Example shown in the image.

The "+" symbol indicates one or more repetitions; in the example only "4" appears due to greedy mode.

2. ? – non‑greedy (optional) match

To match "2004", add "?" after the pattern, resulting in a non‑greedy match that captures the full number.

3. {n} – exact repetition

Using curly braces you can specify the exact number of digits, e.g., {4} matches a four‑digit number.

Below is a quick recap of common regex special characters:

^ – anchors the start of a string.

* – matches the preceding element zero or more times.

. – matches any character.

$ – anchors the end of a string.

? – makes the preceding token optional (non‑greedy mode).

+ – matches one or more repetitions (excluding zero).

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

| – logical OR between alternatives.

[] , [A-Za-z0-9] , [^] – character classes, ranges, and negation.

\s and \S – match whitespace and non‑whitespace respectively.

\w and \W – match word characters (letters, digits, underscore) and their complement.

[\u4E00-\u9FA5] – matches Chinese characters.

() – captures groups for extraction.

These symbols form the foundation for building powerful pattern matching in Python.

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.

PythonTutorialprogramming fundamentalsregexSpecial 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.