7

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?

1
  • It would be great to see an example solution that actully matches \\d or some variant of this as I am still struggling Commented Sep 10, 2015 at 5:53

2 Answers 2

15

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.

Sign up to request clarification or add additional context in comments.

Comments

9

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.

1 Comment

The Text.Regex.Posix module uses foreign function bindings to the POSIX regex C API, I believe. So you are very likely correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.