Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Text.Regex.Posix =~ matches characters with \d?

Can someone explain how this interaction in ghci is possible?

*Main Text.Regex.Posix> "d1" =~ "\\d" :: String
"d"
*Main Text.Regex.Posix> "d1" =~ "\\d" :: Int
1

I thought that \d standed for digit, so I don't understand how it can match the character d and not the character 1 present on the string.

Note: Using ghci 7.10.3

like image 230
João Rodrigues Avatar asked Jan 04 '23 23:01

João Rodrigues


1 Answers

The Text.Regex.Posix module only supports c posix regex api. POSIX regex syntax does not define \d as a shorthand character class for digits, instead, it has [:digit:] POSIX character class that must be used inside bracket expressions, e.g. [[:digit:]]. However, it is as easier and shorter to use [0-9] to match regular ASCII digits.

To use \d in your patterns, you may want to use Text.Regex.PCRE.

like image 112
Wiktor Stribiżew Avatar answered Jan 10 '23 21:01

Wiktor Stribiżew