Safe Haskell | None |
---|---|
Language | Haskell2010 |
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"
- 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
Documentation
:: Extract' a | |
=> Int | group idx. 1-based |
-> (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
:: 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