Haskell's raw string for regex

It seems I am having trouble creating a regex in Haskell, what I'm trying to do is convert this string (which matches the url in the text fragment)

\b(((\S+)?)(@|mailto\:|(news|(ht|f)tp(s?))\://)\S+)\b

In regex, the problem is that I keep getting this error in ghci

Prelude Text.RegExp> let a = fromString "\b(((\S+)?)(@|mailto\:|(news|(ht|f)tp(s?))\://)\S+)\b"

<interactive>:1:27:
    lexical error in string/character literal at character 'S'

I assume this fails because Haskell does not understand it \Sas escape code. Are there any ways around this?

In Scala, you can surround the string with three double quotes, I was wondering if you managed to achieve something similar in Haskell?

Any help would be appreciated.

+5
source share
3 answers

.

"\\b(((\\S+)?)(@|mailto\\:|(news|(ht|f)tp(s?))\\://)\\S+)\\b"

: , . , .

+12

Haskell , GHC quasiquotation:

r :: QuasiQuoter
r = QuasiQuoter {      
    quoteExp  = return . LitE . StringL
    ...
}

:

ghci> :set -XQuasiQuotes
ghci> let s = [r|\b(((\S+)?)(@|mailto\:|(news|(ht|f)tp(s?))\://)\S+)\b|]
ghci> s
"\\b(((\\S+)?)(@|mailto\\:|(news|(ht|f)tp(s?))\\://)\\S+)\\b"

raw-strings-qq Hackage.

+4

Rex:

http://hackage.haskell.org/package/rex

http://hackage.haskell.org/packages/archive/rex/0.4.2/doc/html/Text-Regex-PCRE-Rex.html

which not only uses quasivoting to write with a good regular expression (without double backslashes), it also uses perl-like regular expressions, not the standard annoying POSIX regular expressions, and even allows you to use regular expressions as a template that matches your method parameters, which is a genius.

+1
source

All Articles