I'm using Haskell's Text.Regex
library and I want to match some characters that normally have meaning in regular expressions. According to Text.Regex
's documentation,
The syntax of regular expressions is ... that of egrep (i.e. POSIX "extended" regular expressions).
And apparently, escaping in POSIX Extended Regular Expressions (ERE) uses backslashes [unlike POSIX Basic Regular Expressions (BRE)].
However, when I try to do something like this:
> import Text.Regex
> matchRegex (mkRegex "\*") "*"
I get the following error:
<interactive>:1:23:
lexical error in string/character literal at character '*'
The same thing happens no matter what character I put after the \
.
I could do something like this:
> matchRegex (mkRegex "[*]") "*"
Just []
which works, but it seems like a hack, especially if I want to escape several things in a row (e.g. mkRegex "[[][(][)][]]"
which matches [()]
).
Is this the only way to escape in POSIX ERE? Why doesn't Haskell's Text.Regex
library support \
escaping like it seems it ought to?
I don't know the syntax but usually if you want to write back-slash inside a string you need to escape it, meaning:
matchRegex (mkRegex "\\*") "*"
Does it help?
Try it with two backslashes:
matchRegex (mkRegex "\\*") "*"
I just tried that with GHCI and it worked.
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