How to avoid characters in the Haskell Text.Regex library?

Introduction

I am using the Haskell Text.Regex library and I want to match some characters that usually make sense in regular expressions. According to the Text.Regex documentation ,

Regular expression syntax is ... egrep expression (ie POSIX "extended" regular expressions).

And, apparently, escaping in POSIX Extended Regular Exions (ERE) uses a backslash [as opposed to POSIX Basic Regular Expressions (BRE) .


Problem

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 \ .


workaround

I could do something like this:

 > matchRegex (mkRegex "[*]") "*" Just [] 

but it seems to be a hack, especially if I want to avoid a few things in a row (for example, mkRegex "[[][(][)][]]" that matches [()] ).


Question

Is this the only way to escape to POSIX ERE? Why doesn't the Haskell Text.Regex library support \ , as it seems, it should?

+4
source share
2 answers

I don't know the syntax, but usually, if you want to write a backslash inside a string, you need to avoid it, which means:

 matchRegex (mkRegex "\\*") "*" 

Does it help?

+7
source

Try with two backslashes:

 matchRegex (mkRegex "\\*") "*" 

I just tried this with GHCI and it worked.

+3
source

All Articles