8.6. RegexString: Regex matching made simple

For simple regular expression operations, the Regex library is a little heavyweight. RegexString permits regex matching on ordinary Haskell Strings.

The datatypes and functions that RegexString provides are:

data Regex -- a compiled regular expression

mkRegex
        :: String       -- regexp to compile
        -> Regex        -- compiled regexp

mkRegexWithOpts
        :: String       -- regexp to compile
        -> Bool         -- single line mode?
        -> Bool         -- case insensitive?
        -> Regex

matchRegex
        :: Regex        -- compiled regexp
        -> String       -- string to match
        -> Maybe [String] -- text of $1, $2, ... (if matched)

matchRegexAll
        :: Regex
        -> String
        -> Maybe ( String,  -- before
                   String,  -- entire
                   String,  -- after
                   String,  -- last bracket
                   [String] -- $1..
                 )

mkRegex makes a regular expression with the default options (multi-line, case-sensitive), whereas mkRegexWithOpts takes the values for these options as parameteres. The syntax of regular expressions is otherwise that of Perl.

matchRegexperforms a simple match given a pattern and a string, returning either Nothing if the match failed, or Just [s1...sn] if the match succeeded, where s1...sn are the bracketed components of the pattern in left-to-right order. Note that there may be more components in the returned list than were in the pattern, and that any component which either matched the empty string or wasn't matched at all (because it was part of an optional part of the pattern), will be empty.

matchRegexAll returns more information about the match, including the string before the matched portion and the string after the matched portion.