Home > Article > Backend Development > What is the difference between '\d' and '[0-9]' in Python regular expressions?
[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:
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!