regex-do-1.8: PCRE wrapper

Safe HaskellNone
LanguageHaskell2010

Text.Regex.Do.Pcre.Replace

Synopsis

Documentation

data ReplaceCase Source

Constructors

Once

may be omitted

All

if both Once and All are passed, All prevails

Utf8 
Multiline 

Instances

class Replace r a where Source

dynamic group replace

custom replacer fn returns replacement value

>>> replacer::GroupReplacer String
    replacer = defaultReplacer 1 tweak1
          where tweak1 str1 = case str1 of
                                "101" -> "[сто один]"
                                "3" -> "[three]"
                                otherwise -> trace str1 "?"

Once vs All options

>>> replace [Once,Utf8] (Pattern "\\w=(\\d{1,3})", replacer) $ Body "a=101 b=3 12"

"a=[сто один] b=3 12"

>>> replace [All,Utf8] (Pattern "\\w=(\\d{1,3})", replacer) $ Body "a=101 b=3 12"

"a=[сто один] b=[three] 12"

static replace for simple (no group) needle

for no-regex ByteString replacement see Text.Regex.Do.Split

>>> replace [Once,Utf8] (Pattern "менее", Replacement  "более") $ Body "менее менее"

"более менее"

>>> replace [Once,Utf8] (Pattern "^a\\s", Replacement "A") $ Body "a bc хол.гор."

"Abc хол.гор."

Methods

replace :: [ReplaceCase] -> Pattern a -> r a -> Body a -> a Source

replaceMatch Source

Arguments

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

defaultReplacer Source

Arguments

:: (Replace_ a, 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

type Mr_ a out = (Match a a out, Rx_ a a, Replace_ a, Opt_ a) Source