Is the "d" in Regular Expressions Equivalent to a Digit?
In regular expressions, the character class "d" signifies a digit. However, a recent discovery reveals that in the specific case of Python style regex, "d" demonstrates slightly different behavior.
While examining the number 123, it was observed that "d" matches the digits 1 and 3, but not 2. This observation raises the question of what specific requirements the digit being matched by "d" must meet.
In Python style regex, "d" matches not only the traditional Western digits 0-9, but also digits from other languages and alphabets. In particular, this means that "d" can match characters representing Eastern Arabic numerals (٠١٢٣٤٥٦٧٨٩).
It is important to note that the character class "[0-9]" is not always equivalent to "d". In Python 3, [0-9] matches only the Western digits 0-9, while "d" matches both Western and Eastern Arabic digits. Therefore, in scenarios where you need to match all types of digits, including digits from languages other than English, "d" is a more appropriate choice.
The above is the detailed content of Does '\d' in Python Regex Match Only Western Digits?. For more information, please follow other related articles on the PHP Chinese website!