Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

\d versus [0-9] in .NET regular expressions [duplicate]

Tags:

.net

regex

I often see [0-9] used in .NET regular expression answers on Stack Overflow instead of \d. I’ve asked why, and the answer tends to be “\d matches more than just [0-9]”. So what more does it match? This table says it matches decimal digits. And what about \p{Nd}?

Or is there no difference, and this is just good practice because of some other regex engine?

like image 918
Ry- Avatar asked Jun 19 '13 15:06

Ry-


1 Answers

I think the answer is also in the linked reference:

\d matches any decimal digit. It is equivalent to the \p{Nd} regular expression pattern, which includes the standard decimal digits 0-9 as well as the decimal digits of a number of other character sets.

So \d can match things like decimal digits in the Arabic character set, which wouldn't be matched with [0-9].

like image 197
Myk Willis Avatar answered Oct 20 '22 00:10

Myk Willis