Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

ruby

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?

like image 631
Severin Avatar asked Nov 24 '25 19:11

Severin


1 Answers

\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
like image 164
toro2k Avatar answered Nov 26 '25 14:11

toro2k