Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell regex syntax

Tags:

regex

haskell

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?

like image 312
So8res Avatar asked Dec 14 '11 17:12

So8res


2 Answers

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.

like image 129
jmg Avatar answered Sep 22 '22 02:09

jmg


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.

like image 32
pholser Avatar answered Sep 22 '22 02:09

pholser