What’s the best way to use regular expressions with options (flags) in Haskell
I use
Text.Regex.PCRE
The documentation lists a few interesting options like compCaseless, compUTF8, …
But I don’t know how to use them with (=~)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
All the
Text.Regex.*modules make heavy use of typeclasses, which are there for extensibility and “overloading”-like behavior, but make usage less obvious from just seeing types.Now, you’ve probably been started off from the basic
=~matcher.To use
=~, there must exist an instance ofRegexMaker ...for the LHS, andRegexContext ...for the RHS and result.A valid instance of all these classes (for example,
regex=Regex,compOpt=CompOption,execOpt=ExecOption, andsource=String) means it’s possible to compile aregexwithcompOpt,execOptoptions from some formsource. (Also, given someregextype, there is exactly onecompOpt,execOptset that goes along with it. Lots of differentsourcetypes are okay, though.)A valid instance of all these classes (for example,
regex=Regex,source=String,target=Bool) means it’s possible to match asourceand aregexto yield atarget. (Other validtargets given these specificregexandsourceareInt,MatchResult String,MatchArray, etc.)Put these together and it’s pretty obvious that
=~and=~~are simply convenience functionsand also that
=~and=~~leave no room to pass various options tomakeRegexOpts.You could make your own
which could be used like
or overwrite
=~and=~~with methods which can accept optionsor you could just use
match,makeRegexOpts, etc. directly where needed.