[project @ 2002-04-24 16:31:37 by simonmar]
[packages/base.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/core/LICENSE)
6 --
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable (only on platforms that provide a regex lib)
10 --
11 -- $Id: Regex.hs,v 1.2 2002/04/24 16:31:46 simonmar Exp $
12 --
13 -- Regular expression matching.
14 -- Uses the POSIX regular expression interface in Text.Regex.Posix for now.
15 --
16 -----------------------------------------------------------------------------
17
18 module Text.Regex (
19 Regex,
20 mkRegex,
21 mkRegexWithOpts,
22 matchRegex,
23 matchRegexAll
24 ) where
25
26 import Prelude
27 import qualified Text.Regex.Posix as RE
28 import System.IO.Unsafe
29
30 type Regex = RE.Regex
31
32 mkRegex :: String -> Regex
33 mkRegex s = unsafePerformIO (RE.regcomp s RE.regExtended)
34
35 mkRegexWithOpts :: String -> Bool -> Bool -> Regex
36 mkRegexWithOpts s single_line case_sensitive
37 = unsafePerformIO (RE.regcomp s (RE.regExtended + newline + igcase))
38 where
39 newline | single_line = 0
40 | otherwise = RE.regNewline
41
42 igcase | case_sensitive = 0
43 | otherwise = RE.regIgnoreCase
44
45 matchRegex :: Regex -> String -> Maybe [String]
46 matchRegex p str =
47 case (unsafePerformIO (RE.regexec p str)) of
48 Nothing -> Nothing
49 Just (before, match, after, sub_strs) -> Just sub_strs
50
51 matchRegexAll :: Regex -> String ->
52 Maybe ( String, -- $`
53 String, -- $&
54 String, -- $'
55 [String] -- $1..
56 )
57 matchRegexAll p str = unsafePerformIO (RE.regexec p str)
58