Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- data Replace
- replace :: Regex -> Replace -> Text -> Text
- replace' :: Replace -> Match -> Text
- replaceAll :: Regex -> Replace -> Text -> Text
- replaceAll' :: Replace -> [Match] -> Text
- rgroup :: Int -> Replace
- rtext :: Text -> Replace
- rstring :: String -> Replace
- rfn :: (Match -> Builder) -> Replace
- rtfn :: (Match -> Text) -> Replace
- rbuilder :: Builder -> Replace
OverloadedStrings
Syntax
The syntax used with OverloadedStrings
is meant to be similar to that
used in other regular expression libraries in other programming
languages.
Generally, input text is considered to be static.
>>>
replaceAll "a" "b" "aaa"
"bbb">>>
replaceAll "ab" "ba" "cdababcd"
"cdbabacd"
However, groups from the regular expression's matches can be insert
using $1
(to insert the first group) or ${7}
(to insert the seventh
group).
>>>
replaceAll "(.*), (.*)" "$2 $1" "Beeblebrox, Zaphod"
"Zaphod Beeblebrox">>>
replaceAll "4(\\d)" "${1}4" "7458"
"7548"
Dollar signs can be included in the output by doubling them ($$
).
>>>
replaceAll "(\\d+\\.\\d+)" "$$$1" "9.99"
"$9.99"
Types
A Replace
instance is a function from a regular expression match to
a Builder
. This naturally forms a Monoid
, so
they're easy to combine.
Replace
also implements IsString
, so raw strings can be used to
construct them.
High-level interface
:: Regex | The regular expression to match. |
-> Replace | The specification to replace it with. |
-> Text | The text to operate on. |
-> Text | The text with the first match replaced. |
Execute a regular expression on a Text
and replace the
first match.
:: Replace | The specification to replace it with. |
-> Match | The match to replace. |
-> Text | The text with the match replaced. |
Replace one regular expression match with the Replace
.
:: Regex | The regular expression to match. |
-> Replace | The specification to replace it with. |
-> Text | The text to operate on. |
-> Text | The text with all matches replaced. |
Execute a regular expression on a Text
and replace all
matches.
:: Replace | The specification to replace it with. |
-> [Match] | The matches to replace. |
-> Text | The text with all matches replaced. |
Replace all regular expression matches with the Replace
.
Low-level interface
:: Int | The number of the group in a regular expression. |
-> Replace | The |
Create a Replace
that inserts a regular expression group.