[Haskell-cafe] Regular Expression with PCRE

Joseph Bozeman tasuret at gmail.com
Fri Mar 16 21:55:37 CET 2012


Hey everyone, I'm hoping someone can point me in the right direction.

The regex-pcre package exports (=~) and (=~~) as two useful infix
functions. They're great! The only problem is, they are a *positive* match
for a regex. I have a file that contains HTML comments (it was generated in
Word) and I really just want the barest text. I already have a function
that strips out all the tags, and I have a function that finds all the
links and sticks those in another file for later perusal.

What I'd like is advice on how to implement the (!~) and (!~~) operators.
They should have the same types as (=~) and (=~~). I'm stuck, though.
Here's the source for both of those functions: they depend on
Text.Rege.PCRE package.

(=~) :: (RegexMaker<http://hackage.haskell.org/packages/archive/regex-base/0.93.2/doc/html/Text-Regex-Base-RegexLike.html#t:RegexMaker>
 Regex<http://hackage.haskell.org/packages/archive/regex-pcre/0.94.2/doc/html/Text-Regex-PCRE-Wrap.html#t:Regex>
 CompOption<http://hackage.haskell.org/packages/archive/regex-pcre/0.94.2/doc/html/Text-Regex-PCRE-Wrap.html#t:CompOption>
 ExecOption<http://hackage.haskell.org/packages/archive/regex-pcre/0.94.2/doc/html/Text-Regex-PCRE-Wrap.html#t:ExecOption>
 source, RegexContext<http://hackage.haskell.org/packages/archive/regex-base/0.93.2/doc/html/Text-Regex-Base-RegexLike.html#t:RegexContext>
 Regex<http://hackage.haskell.org/packages/archive/regex-pcre/0.94.2/doc/html/Text-Regex-PCRE-Wrap.html#t:Regex>
source1
target) => source1 -> source -> target

(=~) x r = let q :: Regex               q = makeRegex r           in match q x


(=~~) :: (RegexMaker<http://hackage.haskell.org/packages/archive/regex-base/0.93.2/doc/html/Text-Regex-Base-RegexLike.html#t:RegexMaker>
 Regex<http://hackage.haskell.org/packages/archive/regex-pcre/0.94.2/doc/html/Text-Regex-PCRE-Wrap.html#t:Regex>
 CompOption<http://hackage.haskell.org/packages/archive/regex-pcre/0.94.2/doc/html/Text-Regex-PCRE-Wrap.html#t:CompOption>
 ExecOption<http://hackage.haskell.org/packages/archive/regex-pcre/0.94.2/doc/html/Text-Regex-PCRE-Wrap.html#t:ExecOption>
 source, RegexContext<http://hackage.haskell.org/packages/archive/regex-base/0.93.2/doc/html/Text-Regex-Base-RegexLike.html#t:RegexContext>
 Regex<http://hackage.haskell.org/packages/archive/regex-pcre/0.94.2/doc/html/Text-Regex-PCRE-Wrap.html#t:Regex>
source1
target, Monad<http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Control-Monad.html#t:Monad>
m)
=> source1 -> source -> m target

(=~~) x r = do (q :: Regex) <-  makeRegexM r               matchM q x

What I figured I could do was find a function that was the inverse of
"match" and "matchM", but I can't find any in the docs. I really hope
I don't have to implement *that*, too. I'm still new at this, and that
seems like it would be over my head.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120316/879dffa4/attachment.htm>


More information about the Haskell-Cafe mailing list