I'm having some trouble with regexes in haskell. Specifically:
Prelude Text.Regex.Posix> "1" =~ "\d" :: Bool
<interactive>:1:10:
lexical error in string/character literal at character 'd'
Prelude Text.Regex.Posix> "1" =~ "\\d" :: Bool
False
Prelude Text.Regex.Posix> "1" =~ "\\\\d" :: Bool
False
Does Haskell not have the \d
or \s
or other such convenient escape codes? Yes, I know I can do [0-9]
instead, but the escape codes can be so much more convienient for complex regexes. Am I missing something obvious here?
No, the Haskell language does not have escape sequences like \d
and \s
or regular expressions in general. There are just some libraries which provide regular expressions.
Therefore, you have to look up whether the regex library you are using supports \d
and \s
. And when they do support it, you have to write them as "\\d"
in a Haskell source file.
When \d
stems from Perl you might be more successful with regex-pcre
.
I don't know much about the Haskell regex packages, but from your examples above with "Text.Regex.Posix", I might infer that you're dealing in POSIX regular expressions. Escape sequences such as \d and \s aren't part of POSIX regex syntax, and I believe originated with Perl and have since propagated into other languages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With