Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
- class ReplaceOpen f r 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
Documentation
class ReplaceOpen f r where Source
>>>
replace (Just [(4,3)::PosLen]) (Replacement "4567") (Body "abc 123 def"::Body Text)
"abc 4567 def"
>>>
replacer::GroupReplacer Text
replacer = defaultReplacer 1 tweak1 -- 1: first match in group where tweak1 str1 = case str1 of "123" -> "[1-2-3]" otherwise -> traceShow str1 "?"
>>>
replace (Just ([(4,3),(8,2)]::[PosLen])) replacer (Body "abc 123 def"::Body Text)
"abc [1-2-3] def"
:: Extract' a | |
=> Int | group idx |
-> (a -> a) | (group match -> replacement) tweak |
-> GroupReplacer a |
Replaces specified (by idx) group match with tweaked value. Works for one common simple use case
GroupReplacer
can be used with complicated 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
:: 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