regex-do-2.4: PCRE wrapper

Safe HaskellSafe
LanguageHaskell2010

Text.Regex.Do.Pcre.ReplaceOpen

Description

extensible and reusable replacement functions

Run replacement with your preferred content types e.g. Data.Text, from search results with non-PCRE regex or non-regex libs

open an issue or a PR on git to request a new Extract' instance

Data.Text instance already works

Synopsis

Documentation

class ReplaceOpen f r where Source

Replacement:

>>> replace (Just [(4,3)::PosLen]) (Replacement "4567") (Body "abc 123 def"::Body Text)

"abc 4567 def"

GroupReplacer :

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

Methods

replace :: (Extract' a, ToArray arr) => f arr -> r a -> Body a -> a Source

defaultReplacer Source

Arguments

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

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

call from your custom GroupReplacer passed to replaceGroup

see defaultReplacer source for use example