Home  >  Article  >  Backend Development  >  What is the difference between '\d' and '[0-9]' in Python regular expressions?

What is the difference between '\d' and '[0-9]' in Python regular expressions?

DDD
DDDOriginal
2024-11-10 05:38:02374browse

What is the difference between

[Characters]: d in Regex

The "d" character class in Python style regular expressions is used to match digits. A digit is typically defined as a numeric character (0-9). However, it's worth noting that the behavior of "d" can vary depending on the programming language and implementation.

In the case of Python, "d" matches characters that satisfy the Unicode's p{Nd} property, which includes the following:

  • [0-9] - ASCII digits
  • À-Þ, à-þ - Latin-1 digits
  • 0-9 - CJK digits
  • ٠-٩ - Arabic-Indic digits

Observation:

You mentioned that in the sequence "123", "d" matches "1" and "3" but not "2". This is because Python's "d" matches Unicode digits, which only include select characters like "1" and "3" in a certain context. The character "2" in this case would not be recognized as a Unicode digit.

To match any digit character, regardless of context or language, you should use "[0-9]" instead.

The above is the detailed content of What is the difference between '\d' and '[0-9]' in Python regular expressions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn