| 1 |
{-# LANGUAGE NoImplicitPrelude #-} |
| 2 |
{-# LANGUAGE MultiParamTypeClasses #-} |
| 3 |
{-# LANGUAGE FlexibleContexts #-} |
| 4 |
{-# LANGUAGE FlexibleInstances #-} |
| 5 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 6 |
{-# OPTIONS_GHC -fno-warn-duplicate-exports #-} |
| 7 |
{-# LANGUAGE CPP #-} |
| 8 |
#if __GLASGOW_HASKELL__ >= 800 |
| 9 |
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-} |
| 10 |
{-# OPTIONS_GHC -fno-warn-unused-imports #-} |
| 11 |
#endif |
| 12 |
|
| 13 |
module Text.RE.TDFA.ByteString.Lazy |
| 14 |
( |
| 15 |
-- * Tutorial |
| 16 |
-- $tutorial |
| 17 |
|
| 18 |
-- * The 'Matches' and 'Match' Operators |
| 19 |
(*=~) |
| 20 |
, (?=~) |
| 21 |
-- * The 'SearchReplace' Operators |
| 22 |
, (*=~/) |
| 23 |
, (?=~/) |
| 24 |
-- * The 'Matches' Type |
| 25 |
, Matches |
| 26 |
, matchesSource |
| 27 |
, allMatches |
| 28 |
, anyMatches |
| 29 |
, countMatches |
| 30 |
, matches |
| 31 |
-- * The 'Match' Type |
| 32 |
, Match |
| 33 |
, matchSource |
| 34 |
, matched |
| 35 |
, matchedText |
| 36 |
-- * The Macros and Parsers |
| 37 |
-- $macros |
| 38 |
, module Text.RE.TestBench.Parsers |
| 39 |
-- * The 'RE' Type |
| 40 |
, RE |
| 41 |
, reSource |
| 42 |
-- * Options |
| 43 |
-- $options |
| 44 |
, SimpleREOptions(..) |
| 45 |
-- * Compiling and Escaping REs |
| 46 |
, SearchReplace(..) |
| 47 |
, compileRegex |
| 48 |
, compileRegexWith |
| 49 |
, compileSearchReplace |
| 50 |
, compileSearchReplaceWith |
| 51 |
, escape |
| 52 |
, escapeWith |
| 53 |
, escapeREString |
| 54 |
-- * The Classic regex-base Match Operators |
| 55 |
, (=~) |
| 56 |
, (=~~) |
| 57 |
-- * The re Quasi Quoters |
| 58 |
-- $re |
| 59 |
, re |
| 60 |
, reMultilineSensitive |
| 61 |
, reMultilineInsensitive |
| 62 |
, reBlockSensitive |
| 63 |
, reBlockInsensitive |
| 64 |
, reMS |
| 65 |
, reMI |
| 66 |
, reBS |
| 67 |
, reBI |
| 68 |
, re_ |
| 69 |
-- * The Ed Quasi Quoters |
| 70 |
-- $ed |
| 71 |
, edMultilineSensitive |
| 72 |
, edMultilineInsensitive |
| 73 |
, edBlockSensitive |
| 74 |
, edBlockInsensitive |
| 75 |
, ed |
| 76 |
, edMS |
| 77 |
, edMI |
| 78 |
, edBS |
| 79 |
, edBI |
| 80 |
, ed_ |
| 81 |
-- * The cp Quasi Quoters |
| 82 |
, cp |
| 83 |
-- * IsRegex |
| 84 |
-- $isregex |
| 85 |
, module Text.RE.Tools.IsRegex |
| 86 |
) where |
| 87 |
|
| 88 |
import Control.Monad.Fail |
| 89 |
import qualified Data.ByteString.Lazy.Char8 as LBS |
| 90 |
import Data.Typeable |
| 91 |
import Prelude.Compat |
| 92 |
import Text.RE.REOptions |
| 93 |
import Text.RE.Replace |
| 94 |
import Text.RE.TestBench.Parsers |
| 95 |
import Text.RE.Tools.IsRegex |
| 96 |
import Text.RE.ZeInternals |
| 97 |
import Text.RE.ZeInternals.SearchReplace.TDFA.ByteString.Lazy |
| 98 |
import Text.RE.ZeInternals.TDFA |
| 99 |
import Text.Regex.Base |
| 100 |
import qualified Text.Regex.TDFA as TDFA |
| 101 |
-- NB regex-base instance imports maybe be needed for for some API modules |
| 102 |
|
| 103 |
-- | find all the matches in the argument text; e.g., to count the number |
| 104 |
-- of naturals in s: |
| 105 |
-- |
| 106 |
-- @countMatches $ s *=~ [re|[0-9]+|]@ |
| 107 |
-- |
| 108 |
(*=~) :: LBS.ByteString |
| 109 |
-> RE |
| 110 |
-> Matches LBS.ByteString |
| 111 |
(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ match (reRegex rex) bs |
| 112 |
|
| 113 |
-- | find the first match in the argument text; e.g., to test if there |
| 114 |
-- is a natural number in the input text: |
| 115 |
-- |
| 116 |
-- @matched $ s ?=~ [re|[0-9]+|]@ |
| 117 |
-- |
| 118 |
(?=~) :: LBS.ByteString |
| 119 |
-> RE |
| 120 |
-> Match LBS.ByteString |
| 121 |
(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ match (reRegex rex) bs |
| 122 |
|
| 123 |
-- | search and replace all matches in the argument text; e.g., this section |
| 124 |
-- will convert every YYYY-MM-DD format date in its argument text into a |
| 125 |
-- DD\/MM\/YYYY date: |
| 126 |
-- |
| 127 |
-- @(*=~\/ [ed|${y}([0-9]{4})-0*${m}([0-9]{2})-0*${d}([0-9]{2})\/\/\/${d}\/${m}\/${y}|])@ |
| 128 |
-- |
| 129 |
(*=~/) :: LBS.ByteString -> SearchReplace RE LBS.ByteString -> LBS.ByteString |
| 130 |
(*=~/) = flip searchReplaceAll |
| 131 |
|
| 132 |
-- | search and replace the first occurrence only (if any) in the input text |
| 133 |
-- e.g., to prefix the first string of four hex digits in the input text, |
| 134 |
-- if any, with @0x@: |
| 135 |
-- |
| 136 |
-- @(?=~\/ [ed|[0-9A-Fa-f]{4}\/\/\/0x$0|])@ |
| 137 |
-- |
| 138 |
(?=~/) :: LBS.ByteString -> SearchReplace RE LBS.ByteString -> LBS.ByteString |
| 139 |
(?=~/) = flip searchReplaceFirst |
| 140 |
|
| 141 |
-- | the `regex-base` polymorphic match operator |
| 142 |
(=~) :: ( Typeable a |
| 143 |
, RegexContext TDFA.Regex LBS.ByteString a |
| 144 |
) |
| 145 |
=> LBS.ByteString |
| 146 |
-> RE |
| 147 |
-> a |
| 148 |
(=~) bs rex = addCaptureNames (reCaptureNames rex) $ match (reRegex rex) bs |
| 149 |
|
| 150 |
-- | the `regex-base` monadic, polymorphic match operator |
| 151 |
(=~~) :: ( Monad m, MonadFail m |
| 152 |
, Functor m |
| 153 |
, Typeable a |
| 154 |
, RegexContext TDFA.Regex LBS.ByteString a |
| 155 |
) |
| 156 |
=> LBS.ByteString |
| 157 |
-> RE |
| 158 |
-> m a |
| 159 |
(=~~) bs rex = addCaptureNames (reCaptureNames rex) <$> matchM (reRegex rex) bs |
| 160 |
|
| 161 |
instance IsRegex RE LBS.ByteString where |
| 162 |
matchOnce = flip (?=~) |
| 163 |
matchMany = flip (*=~) |
| 164 |
makeRegexWith = \o -> compileRegexWith o . unpackR |
| 165 |
makeSearchReplaceWith = \o r t -> compileSearchReplaceWith o (unpackR r) (unpackR t) |
| 166 |
regexSource = packR . reSource |
| 167 |
|
| 168 |
-- $tutorial |
| 169 |
-- We have a regex tutorial at <http://tutorial.regex.uk>. |
| 170 |
|
| 171 |
-- $macros |
| 172 |
-- There are a number of RE macros and corresponding Haskell parsers |
| 173 |
-- for parsing the matched text into appropriate Haskell types. See |
| 174 |
-- the [Macros Tables](http://regex.uk/macros) for details. |
| 175 |
|
| 176 |
-- $options |
| 177 |
-- You can specify different compilation options by appending a |
| 178 |
-- to the name of an [re| ... |] or [ed| ... \/\/\/ ... |] quasi quoter |
| 179 |
-- to select the corresponding compilation option. For example, the |
| 180 |
-- section, |
| 181 |
-- |
| 182 |
-- @(?=~/ [edBlockInsensitive|foo$\/\/\/bar|])@ |
| 183 |
-- |
| 184 |
-- will replace a @foo@ suffix of the argument text, of any |
| 185 |
-- capitalisation, with a (lower case) @bar@. If you need to specify the |
| 186 |
-- options dynamically, use the @[re_| ... |]@ and @[ed_| ... \/\/\/ ... |]@ |
| 187 |
-- quasi quoters, which generate functions that take an 'IsOption' option |
| 188 |
-- (e.g., a 'SimpleReOptions' value) and yields a 'RE' or 'SearchReplace' |
| 189 |
-- as appropriate. For example if you have a 'SimpleReOptions' value in |
| 190 |
-- @sro@ then |
| 191 |
-- |
| 192 |
-- @(?=~/ [ed_|foo$\/\/\/bar|] sro)@ |
| 193 |
-- |
| 194 |
-- will compile the @foo$@ RE according to the value of @sro@. For more |
| 195 |
-- on specifying RE options see "Text.RE.REOptions". |
| 196 |
|
| 197 |
-- $re |
| 198 |
-- The @[re|.*|]@ quasi quoters, with variants for specifying different |
| 199 |
-- options to the RE compiler (see "Text.RE.REOptions"), and the |
| 200 |
-- specialised back-end types and functions. |
| 201 |
|
| 202 |
-- $ed |
| 203 |
-- The @[ed|.*\/\/\/foo|]@ quasi quoters, with variants for specifying different |
| 204 |
-- options to the RE compiler (see "Text.RE.REOptions"). |
| 205 |
|
| 206 |
-- $ed |
| 207 |
-- The -- | the @[ed| ... \/\/\/ ... |]@ quasi quoters; for example, |
| 208 |
-- |
| 209 |
-- @[ed|${y}([0-9]{4})-0*${m}([0-9]{2})-0*${d}([0-9]{2})\/\/\/${d}\/${m}\/${y}|])@ |
| 210 |
-- |
| 211 |
-- represents a @SearchReplace@ that will convert a YYYY-MM-DD format date |
| 212 |
-- into a DD\/MM\/YYYY format date. |
| 213 |
-- |
| 214 |
-- The only difference between these quasi quoters is the RE options that are set, |
| 215 |
-- using the same conventions as the @[re| ... |]@ quasi quoters. |
| 216 |
|
| 217 |
-- $isregex |
| 218 |
-- The 'IsRegex' class is used to abstract over the different regex back ends and |
| 219 |
-- the text types they work with -- see "Text.RE.Tools.IsRegex" for details. |