[Haskell] regular expression syntax - perl ain't got nothin on haskell

John Meacham john at repetae.net
Tue Feb 24 13:39:15 EST 2004


I have updated my code as well as made a simple template haskell
version.

A Wikified homepage can be found at
 http://haskell.org/hawiki/RegexSyntax

I have found that the syntax combines really really really well with
pattern guards.

f s  | Just 3 <- s =~~ "i/john" = print "exactly 3 johns found."
     | False <- s =~ "ba*r" = print "no baaaaaaars found"
     | Just a <- s =~~ "([a-z]*)/(.*)/([a-z]*)" = print $ 
          concat [a!1 ++ a!3, " are the flags. ", a!2, " is the expression"]
f _ = print "nothing found!"

I am still tweaking the code so expect it to change without notice over
the next few days.

the template haskell version can be used by importing RegexTH then doing
 s =~ $(mkRE "i/john") 
right now, all it does is check syntax and give an error if the regular
expression is invalid at compile time, but in the future, it will
probably build optimized matchers at compile time. 

A problem is still that lots of type annotations are needed to resolve
the overloading, but it is still okay to use, if anyone has any
suggestions on mitigating this, then that would be good. something i
have found to be useful, is to use the minimal return type necessary for
the task at hand, since any unmatched values will probably need type
annotations.

        John

-- 
---------------------------------------------------------------------------
John Meacham - California Institute of Technology, Alum. - john at foo.net
---------------------------------------------------------------------------


More information about the Haskell mailing list