regex-do-1.4: 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 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

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

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

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

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

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

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

"Abc хол.гор."

Minimal complete definition

Nothing

Methods

replace :: Mr_ a => [ReplaceCase] -> Pattern a -> Replacement a -> Body a -> a Source

replaceGroup :: Mr_ a => [ReplaceCase] -> Pattern a -> GroupReplacer 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 example replacer above

defaultReplacer Source

Arguments

:: (Replace_ a, Extract a) 
=> Int

group idx

-> (a -> a)

(group match -> replacement) tweak

-> GroupReplacer a 

you can write a custom replacer. This is only one common use case.

Replaces specified (by idx) group match with tweaked value.

getGroup :: Extract a => ReplaceAcc a -> MatchArray -> Int -> Maybe a Source

get group content safely

call from your custom GroupReplacer passed to replaceGroup

type Mr_ a = (Match Regex a, Replace_ a, Opt_ a) Source