Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 248789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:22:21+00:00 2026-05-11T21:22:21+00:00

What’s the best way to use regular expressions with options (flags) in Haskell I

  • 0

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 (=~)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:22:21+00:00Added an answer on May 11, 2026 at 9:22 pm

    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.

    (=~) ::
      ( RegexMaker Regex CompOption ExecOption source
      , RegexContext Regex source1 target )
      => source1 -> source -> target
    (=~~) ::
      ( RegexMaker Regex CompOption ExecOption source
      , RegexContext Regex source1 target, Monad m )
      => source1 -> source -> m target
    

    To use =~, there must exist an instance of RegexMaker ... for the LHS, and RegexContext ... for the RHS and result.

    class RegexOptions regex compOpt execOpt | ...
          | regex -> compOpt execOpt
          , compOpt -> regex execOpt
          , execOpt -> regex compOpt
    class RegexOptions regex compOpt execOpt
          => RegexMaker regex compOpt execOpt source
             | regex -> compOpt execOpt
             , compOpt -> regex execOpt
             , execOpt -> regex compOpt
      where
        makeRegex :: source -> regex
        makeRegexOpts :: compOpt -> execOpt -> source -> regex
    

    A valid instance of all these classes (for example, regex=Regex, compOpt=CompOption, execOpt=ExecOption, and source=String) means it’s possible to compile a regex with compOpt,execOpt options from some form source. (Also, given some regex type, there is exactly one compOpt,execOpt set that goes along with it. Lots of different source types are okay, though.)

    class Extract source
    class Extract source
          => RegexLike regex source
    class RegexLike regex source
          => RegexContext regex source target
      where
        match :: regex -> source -> target
        matchM :: Monad m => regex -> source -> m target
    

    A valid instance of all these classes (for example, regex=Regex, source=String, target=Bool) means it’s possible to match a source and a regex to yield a target. (Other valid targets given these specific regex and source are Int, MatchResult String, MatchArray, etc.)

    Put these together and it’s pretty obvious that =~ and =~~ are simply convenience functions

    source1 =~ source
      = match (makeRegex source) source1
    source1 =~~ source
      = matchM (makeRegex source) source1
    

    and also that =~ and =~~ leave no room to pass various options to makeRegexOpts.

    You could make your own

    (=~+) ::
       ( RegexMaker regex compOpt execOpt source
       , RegexContext regex source1 target )
       => source1 -> (source, compOpt, execOpt) -> target
    source1 =~+ (source, compOpt, execOpt)
      = match (makeRegexOpts compOpt execOpt source) source1
    (=~~+) ::
       ( RegexMaker regex compOpt execOpt source
       , RegexContext regex source1 target, Monad m )
       => source1 -> (source, compOpt, execOpt) -> m target
    source1 =~~+ (source, compOpt, execOpt)
      = matchM (makeRegexOpts compOpt execOpt source) source1
    

    which could be used like

    "string" =~+ ("regex", CompCaseless + compUTF8, execBlank) :: Bool
    

    or overwrite =~ and =~~ with methods which can accept options

    import Text.Regex.PCRE hiding ((=~), (=~~))
    
    class RegexSourceLike regex source
      where
        makeRegexWith source :: source -> regex
    instance RegexMaker regex compOpt execOpt source
             => RegexSourceLike regex source
      where
        makeRegexWith = makeRegex
    instance RegexMaker regex compOpt execOpt source
             => RegexSourceLike regex (source, compOpt, execOpt)
      where
        makeRegexWith (source, compOpt, execOpt)
          = makeRegexOpts compOpt execOpt source
    
    source1 =~ source
      = match (makeRegexWith source) source1
    source1 =~~ source
      = matchM (makeRegexWith source) source1
    

    or you could just use match, makeRegexOpts, etc. directly where needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.