Regex

Regex.hs

Copyright © 2007 Dave Bayer. Subject to a BSD-style license.

This module is part of the Annote project.

module Regex (Regex,mkRegex,matchRegex,Sub,mkSub,doSub,isMatch) where

Regex provides regular expression matching. It is a wrapper around Text.Regex.


Text.Regex implements POSIX extended regular expression matching. We import the following functions:

mkRegex    :: String -> Regex
matchRegex :: Regex -> String -> Maybe [String]
subRegex   :: Regex -> String -> String -> String
import Text.Regex (Regex,mkRegex,matchRegex,subRegex)

Sub

Sub is the type of a substitution pair.

type Sub = (Regex,String)

mkSub

mkSub prepares a substitution pair.

mkSub :: (String,String) → Sub
mkSub (r,s) = (mkRegex r, s)

doSub

doSub applies a substitution pair. The argument order is chosen to facilitate left folds, so that we can apply a list of substitutions in order. This also allows for the idiom

doSub header $ mkSub ("[{]TITLE[}]", title)

For optimized code, mkSub will only be called once.

doSub :: String → Sub → String
doSub input (regex,replace) = subRegex regex input replace

isMatch

isMatch is a predicate, reporting whether or not regex succeeds in matching text. We use the same argument order as doSub.

isMatch :: String → Regex → Bool
isMatch text regex = case matchRegex regex text of
    Nothing → False
    Just _  → True