JRegex

JRegex

This library provides an overloaded perl-like (=~) regular expression operator for haskell. Since haskell type classes allow routines to change their behavior based on the return type of values, this operator ends up being much more powerful than the perl version.

This library also provides an interface to PCRE to use perl compatable regular expressions and a more advanced interface to the Posix regex matcher.

Example

Here are some routines and their output. what is returned depends on the type you ask for

    -- return number of times the regex matches
    print ("hellofooobarbobfoobarbad" =~ "fo*bar" :: Int)
     => 2
    -- returns the first matching expression
    print ("hellofooobarbobfoobarbad" =~ "fo*bar" :: String)
     => "fooobar"
    -- returns the string before the first match, the matching string, and the string after the first match
    print ("hellofooobarbobfoobarbad" =~ "fo*bar" :: (String,String,String))
     => ("hello","fooobar","bobfoobarbad")
    -- returns true if there is a match at all and false otherwise
    print ("hellofooobarbobfoobarbad" =~ "fo*bar" :: Bool)
     => True
    -- always returns (). useful with the (=~~) monadic operator
    print ("hellofooobarbobfoobarbad" =~ "fo*bar" :: ())
     => ()
    -- returns all matching substrings
    print ("hellofooobarbobfoobarbad" =~ "fo*bar" :: [String])
     => ["fooobar","foobar"]
    -- returns an array off all matching parenthesised substrings in the regex for first match
    print ("hellofooobarbobfoobaaarbad" =~ "f(o*)b(a+r)" :: Array Int String)
     => array (0,2) [(0,"fooobar"),(1,"ooo"),(2,"ar")]
    -- returns an array off all matching parenthesised substrings in the regex for all matches
    print ("hellofooobarbobfoobaaarbad" =~ "f(o*)b(a+r)" :: [Array Int String])
     => [array (0,2) [(0,"fooobar"),(1,"ooo"),(2,"ar")],array (0,2) [(0,"foobaaar"),(1,"oo"),(2,"aaar")]]

In addition to (=~), the library provides (=~~) which is a monadic version of (=~) that always behaves in the same way, except it will fail if no match occurs.

(!~) is equivalent to not . (~=)

there are also classes defined so you may declare your own instances of new types of regexs or matchers that act on any data type, not just strings.

Getting It