Home >Backend Development >C++ >Why is `\d` Slower Than `[0-9]` in C# Regex?
Slower than slower d
[0-9]
This article discusses a surprising discovery in the C#regular expression engine:
character range efficiency, even if d
is a super set of [0-9]
. [0-9]
d
The difference between
d
[0-9]
The main reason for the difference in efficiency is the difference between these two characters:
:
d
: d
Only match the number 0-9. It is a compact representation of the character set . [0-9]
[0123456789]
When matching the string with the regular expression, the regular expression engine needs to compare each character in the string with the specified character class. For , it must perform additional inspections to determine whether the character is a unicode number, which may be higher than the simple checking character in the range of 0-9. The test code provided by demonstrates this performance difference. On the more than 10,000 random string (each string contains about 50%of the numbers), the following results are obtained:
d
regular expression
Regular expression : 00: 00: 00.1357972 (Fast)
Regular expression
: 00: 00: 00.1388997 (slightly slower thand
[0-9]
[0123456789]
character range. [0-9]
The above is the detailed content of Why is `\d` Slower Than `[0-9]` in C# Regex?. For more information, please follow other related articles on the PHP Chinese website!