| Safe Haskell | None | 
|---|---|
| Language | Haskell2010 | 
Text.Regex.Do.Replace.Open
Description
extensible and reusable replacement functions
Run replacement with your preferred content types e.g. Data.Text (implemented),
from search results with non-PCRE regex or non-regex libs
how to use:
value replacement:
>>>replace (Just [(4,3)::PosLen]) "4567" ("abc 123 def"::Text)
"abc 4567 def"
GroupReplacer : replace with a function
replacer::GroupReplacer Text
replacer = defaultReplacer 1 tweak1        --  1: group 1 match. 
          where tweak1 str1 = case str1 of
                                "123" -> "[1-2-3]"
                                otherwise -> traceShow str1 "?"
>>>replace (Just ([(4,3)]::[PosLen])) replacer ("abc 123 def"::Text)
"abc [1-2-3] def"
Synopsis
- class Replace f repl body where
 - defaultReplacer :: Extract' a => Int -> (a -> a) -> GroupReplacer a
 - getGroup :: Extract a => ReplaceAcc a -> MatchArray -> Int -> Maybe a
 - replaceMatch :: Extract' a => PosLen -> (a, ReplaceAcc a) -> ReplaceAcc a
 - boundsOk :: MatchArray -> Int -> Bool
 
Documentation
class Replace f repl body where Source #
Instances
| Replace [] b b Source # | |
| Replace Maybe b b Source # | |
| Replace [] (GroupReplacer b) b Source # | |
Defined in Text.Regex.Do.Replace.Open  | |
| Replace Maybe (GroupReplacer b) b Source # | |
Defined in Text.Regex.Do.Replace.Open  | |
Arguments
| :: Extract' a | |
| => Int | group idx. 0: full match, groups: 1.. see   | 
| -> (a -> a) | (group match -> replacement) lookup  | 
| -> GroupReplacer a | 
Replaces specified (by idx) group match with value provided by (a -> a) fn. Works for one common simple use case
GroupReplacer can also be used with multi-group regex
another custom dynamic replacer could e.g. inspect all group matches before looking up a replacement.
getGroup :: Extract a => ReplaceAcc a -> MatchArray -> Int -> Maybe a Source #
get group content safely:
- non-existing group idx will not error but return 
Nothing - adjust for previous replacements length
 
see defaultReplacer source for use example
Arguments
| :: Extract' a | |
| => PosLen | replaceable, unadjusted  | 
| -> (a, ReplaceAcc a) | (new val, acc passed to   | 
| -> ReplaceAcc a | new acc  | 
replace group match while adjusting for previous replacements length
see defaultReplacer source for use example     
boundsOk :: MatchArray -> Int -> Bool Source #
check if specified group index is within MatchArray bounds
for use within GroupReplacer