Severin
Severin

Reputation: 8588

What is the difference between \d and \p{Digit}?

While I have been using \p{Alpha} and \p{Space} for quite some time in my regular expressions I just came across \p{Digit}, but I couldn't find any information about what the up- or downsides are compared to the normal \d that I normally use. What are the key differences between those to?

Upvotes: 3

Views: 728

Answers (1)

toro2k
toro2k

Reputation: 19238

\d matches only ASCII digits, i.e. it is equivalent to the class [0-9]. \p{Digit} matches the same characters as \d plus any other Unicode character that represents a digit. For example to match the arabic zero (code point U+0660):

"\u0660"
# => "٠"

"\u0660" =~ /\d/
# => nil

"\u0660" =~ /\p{Digit}/
# => 0

Upvotes: 9

Related Questions