Explain Codes LogoExplain Codes Logo

Does "\d" in regex mean a digit?

python
regex-engineering
promises
callbacks
Anton ShumikhinbyAnton Shumikhin·Jan 22, 2025
TLDR

Yeah, it does. \d in regex stands for any digit (0-9). In Python:

import re # Let's go hunting for digits! re.findall(r'\d', 'Example123') # ['1', '2', '3'] just got caught!

But don't underestimate \d; this guy matches more than just your vanilla Latin digits. It matches all Unicode digit characters, even non-Latin ones!

When \d goes beyond 0-9

In Python 3, \d doesn't stop at matching 0-9. Its talent extends to catching Unicode digit characters too, even those non-Latin charlatans (no offense!) Here's \d in action with Eastern Arabic numerals (٠‎ ١‎ ٢‎ ٣‎ ٤‎ ٥‎ ٦‎ ٧‎ ٨‎ ٩):

# \d is now multilingual! re.findall(r'\d', '١٢٣') # Returns ['١', '٢', '٣']

Limiting the Scope

But what if you're old school, and all you want is the good old ASCII digits (0-9)? Simply put them in a character class and voila:

# Back to basics. re.findall(r'[0-9]', 'Example١٢٣123') # Returns ['1', '2', '3']...Simple as 123!

The Art of Counting Digits

Wanna catch a bunch of digits, say three, in a row? Pair up \d with quantifiers to go on a digit-spree:

# Gotta catch 'em all...well, just the digits. re.findall(r'\d{3}', 'Example123456') # Returns ['123', '456']

Clarifying the \d Confusion

Misconceptions like "\d matches only odd-indexed digits" are like wild rumors—not entirely reliable. Let's clear them once and for all:

Odd \d Behavior Unmasked

  • Unusual behaviors of \d are usually due to text editor issues or regex plugin quirks, not Python or regex. Think of it as the difference between homemade and store-bought cookies.

  • Terminal highlight settings can play tricks, making the regex look like it's just walked out of a Halloween party. Don't let these phantoms confuse you!

  • When seeking help, always share the full story (i.e., the full regex). You wouldn't diagnose a car problem over the phone, would you?

The Dynamic Duo: \d and Metacharacters

\d paired with other regex metacharacters leads to exceptional detective work:

  • A digit followed by a word character:

    re.findall(r'\d\w', '12abc 34def') # ["These aren't droids. They are" '2a', '4d']
  • A digit fresh at the start:

    re.search(r'^\d', 'abc 123') # Extraterrestrial Life? 'None' re.search(r'^\d', '123 abc') # Extraterrestrial Life! <re.Match object; span=(0, 1), match='1'>