[project @ 2003-08-30 23:01:48 by ross]
[haskell-directory.git] / Text / Regex.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Text.Regex
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/base/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  non-portable (only on platforms that provide a regex lib)
10 --
11 -- Regular expression matching.  Uses the POSIX regular expression
12 -- interface in "Text.Regex.Posix".
13 --
14 -----------------------------------------------------------------------------
15
16 module Text.Regex (
17     -- * Regular expressions
18     Regex,
19     mkRegex,
20     mkRegexWithOpts,
21     matchRegex,
22     matchRegexAll
23   ) where
24
25 import Prelude
26 import qualified Text.Regex.Posix as RE
27 import Text.Regex.Posix ( Regex )
28 import System.IO.Unsafe
29
30 -- | Makes a regular expression with the default options (multi-line,
31 -- case-sensitive).  The syntax of regular expressions is
32 -- otherwise that of @egrep@ (i.e. POSIX \"extended\" regular
33 -- expressions).
34 mkRegex :: String -> Regex
35 mkRegex s = unsafePerformIO (RE.regcomp s RE.regExtended)
36
37 -- | Makes a regular expression, where the multi-line and
38 -- case-sensitve options can be changed from the default settings.
39 mkRegexWithOpts
40    :: String  -- ^ The regular expression to compile
41    -> Bool    -- ^ 'True' @\<=>@ @\'^\'@ and @\'$\'@ match the beginning and 
42               -- end of individual lines respectively, and @\'.\'@ does /not/
43               -- match the newline character.
44    -> Bool    -- ^ 'True' @\<=>@ matching is case-sensitive
45    -> Regex   -- ^ Returns: the compiled regular expression
46
47 mkRegexWithOpts s single_line case_sensitive
48    = unsafePerformIO (RE.regcomp s (RE.regExtended + newline + igcase))
49    where
50         newline | single_line = RE.regNewline
51                 | otherwise   = 0
52
53         igcase  | case_sensitive = 0 
54                 | otherwise      = RE.regIgnoreCase
55
56 -- | Match a regular expression against a string
57 matchRegex
58    :: Regex     -- ^ The regular expression
59    -> String    -- ^ The string to match against
60    -> Maybe [String]    -- ^ Returns: @'Just' strs@ if the match succeeded
61                         -- (and @strs@ is the list of subexpression matches),
62                         -- or 'Nothing' otherwise.
63 matchRegex p str = 
64   case (unsafePerformIO (RE.regexec p str)) of
65         Nothing -> Nothing
66         Just (before, match, after, sub_strs) -> Just sub_strs
67
68 -- | Match a regular expression against a string, returning more information
69 -- about the match.
70 matchRegexAll
71    :: Regex     -- ^ The regular expression
72    -> String    -- ^ The string to match against
73    -> Maybe ( String, String, String, [String] )
74                 -- ^ Returns: 'Nothing' if the match failed, or:
75                 -- 
76                 -- >  Just ( everything before match,
77                 -- >         portion matched,
78                 -- >         everything after the match,
79                 -- >         subexpression matches )
80
81 matchRegexAll p str = unsafePerformIO (RE.regexec p str)
82