Does '\d' in Python Regex Match Only Western Digits?

Barbara Streisand
Release: 2024-11-09 15:03:02
Original
336 people have browsed it

Does

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template