All Text.Regex.* Modules Text.Regex.* heavy use of type classes that exist for extensibility and "overloading" behavior, but make use less obvious from just seeing types.
Now you probably started working with basic =~ .
(=~) :: ( 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 be an instance of RegexMaker ... for LHS and RegexContext ... for RHS and the 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 the ability to compile the regex options with compOpt,execOpt from some form of source . (Also, given some regex type, there is exactly one set of compOpt,execOpt , that comes with it. However, many different types of source all the same.)
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 (e.g. regex=Regex , source=String , target=Bool ) means that it can match a source and a regex to get target . (Other valid target given these specific regex and source are Int , MatchResult String , MatchArray , etc.)
Put them together, and it's pretty obvious that =~ and =~~ are just handy functions
source1 =~ source = match (makeRegex source) source1 source1 =~~ source = matchM (makeRegex source) source1
and also that =~ and =~~ do not leave room for passing various makeRegexOpts parameters.
You can 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 can be used as
"string" =~+ ("regex", CompCaseless + compUTF8, execBlank) :: Bool
or overwrite =~ and =~~ using methods that can take parameters
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 can just use match , makeRegexOpts , etc. if necessary.