Fundamentals 6 min read

Master Python Regex: Understanding Special Characters and Their Uses

This article explains Python's regular expression special characters—such as \d, +, ?, {n}, and []—detailing their meanings, greedy vs. non‑greedy behavior, and how to use quantifiers and character classes effectively, complemented by illustrative examples and images.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Python Regex: Understanding Special Characters and Their Uses

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

1. The special character "\d" is commonly used to represent digits. Example code is shown in the image.

The "+" symbol means one or more repetitions, representing consecutive digits. However, only "4" is output due to greedy mode, as mentioned in the previous article.

2. To match "2004", you need to add the special character "?" to make it non‑greedy, as shown in the image.

In non‑greedy mode, matching proceeds from left to right, capturing consecutive digits, resulting in "2004".

3. Another common usage is using curly braces to specify the length of the matched number, e.g., {4} for exactly four consecutive digits.

We briefly review ten common regex special characters without detailed expansion. ^: 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 quantifier non‑greedy. +: matches one or more repetitions (at least one). {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. (): groups for capturing substrings.

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.