[Haskell-cafe] Re: How does one use Text.Regex.Base.RegexLike?

ChrisK haskell at list.mightyreason.com
Thu Dec 25 21:05:25 EST 2008


Yeah!

I am glad the mailing list has helped.  If you get stuck or need something 
fancier then let me know (I wrote RegexLike).

> But I still don't know how to get makeRegex to work. You need it to specify
> options like case insensitivity, or to use functions like matchAllText.

Well, the options are defined in detail by the back end module, e.g. 
regex-posix.  Take Text.Regex.Posix.Wrap:

Most importantly is the "Regex" data type defined in this module.

Also, there is an instance of "RegexMaker Regex CompOption ExecOption String".

The CompOptions and ExecOptions are defined in this module, and can be passed to 
  "makeRegexOpts" from the RegexMaker class that converts String/ByteString/.. 
to the Regex data type.  The CompOptions are only settable at this compilation 
stage.

Later you can use "getExecOpts" and "setExecOpts" to create a new Regex from the 
original Regex which differs in the value of ExecOptions it uses.  These 
execution options can change after creating the Regex.

The "makeRegex" uses the "defaultCompOpt" and "defaultExecOpt" values.

The CompOptions for Text.Regex.Posix.Wrap are at
http://www.haskell.org/ghc/docs/latest/html/libraries/regex-posix/Text-Regex-Posix-Wrap.html#4
and are just a newtype of the underlying c-library's bitmapped int flags.  AND I 
made this newtype derive the same "Bits" instance, so this works:

> let r = makeRegexOpts (defaultCompOpts || compIgnoreCase)
>                       defaultExecOpts "caseInSensiTIVe"

By binding a Regex value like this and using it several times one saves the 
effort of compiling the String pattern into the Regex data type.

If you need something that is not exactly given by the RegexLike API then there 
may be a RegexContext instance which does give you what you want.

WARNING: The version of regex-base you are using matters most when it comes to 
the RegexContext instances.

The latest 0.93.1 version is at
http://hackage.haskell.org/packages/archive/regex-base/0.93.1/doc/html/Text-Regex-Base-Context.html
and please note the "Source" link in the top right corner area which helps in 
understanding that RegexContext is just a convenience wrapper.

ghc 6.10 is distributed with the 0.72.0.2 version which is at
http://hackage.haskell.org/packages/archive/regex-base/0.72.0.2/doc/html/Text-Regex-Base-Context.html
and again has a link the "Source".

Hooray for hackage.

-- 
Chris



More information about the Haskell-Cafe mailing list