regex-do-3.2.2: PCRE wrapper
Safe HaskellNone
LanguageHaskell2010

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

Documentation

class Replace f repl body where Source #

Methods

replace :: (Extract' body, ToArray arr) => f arr -> repl -> body -> body Source #

Instances

Instances details
Replace [] b b Source # 
Instance details

Defined in Text.Regex.Do.Replace.Open

Methods

replace :: (Extract' b, ToArray arr) => [arr] -> b -> b -> b Source #

Replace Maybe b b Source # 
Instance details

Defined in Text.Regex.Do.Replace.Open

Methods

replace :: (Extract' b, ToArray arr) => Maybe arr -> b -> b -> b Source #

Replace [] (GroupReplacer b) b Source # 
Instance details

Defined in Text.Regex.Do.Replace.Open

Methods

replace :: (Extract' b, ToArray arr) => [arr] -> GroupReplacer b -> b -> b Source #

Replace Maybe (GroupReplacer b) b Source # 
Instance details

Defined in Text.Regex.Do.Replace.Open

Methods

replace :: (Extract' b, ToArray arr) => Maybe arr -> GroupReplacer b -> b -> b Source #

defaultReplacer Source #

Arguments

:: Extract' a 
=> Int

group idx. 0: full match, groups: 1.. see MatchArray

-> (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

replaceMatch Source #

Arguments

:: Extract' a 
=> PosLen

replaceable, unadjusted

-> (a, ReplaceAcc a)

(new val, acc passed to GroupReplacer)

-> 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