"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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?

Published on 2024-11-08
Browse:715

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3