6

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?

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • I would guess `\p{Nd}` matches [these](http://www.fileformat.info/info/unicode/category/Nd/list.htm). – mbeckish Jun 19 '13 at 15:08

1 Answers1

10

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].

Myk Willis
  • 12,306
  • 4
  • 45
  • 62
  • Wow, I can’t believe I didn’t see that section while finding http://msdn.microsoft.com/en-us/library/20bw873z.aspx#SupportedUnicodeGeneralCategories. Thanks. – Ry- Jun 19 '13 at 15:09