text-regex-replace-0.1.1.1: Easy replacement when using text-icu regexes.

Safe HaskellNone
LanguageHaskell2010

Data.Text.ICU.Replace

Contents

Description

This implements a common DSL for regular expression replacement text. This is represented with the Replace data type. It also implements the IsString interface, so if OverloadedStrings is on, you can use a raw string to build the replacement.

Synopsis

OverloadedStrings Syntax

The syntax used with OverloadedStrings is meant to be similar to that used in other regular expression libraries in other programming languages.

Generally, input text is considered to be static.

>>> replaceAll "a" "b" "aaa"
"bbb"
>>> replaceAll "ab" "ba" "cdababcd"
"cdbabacd"

However, groups from the regular expression's matches can be insert using $1 (to insert the first group) or ${7} (to insert the seventh group).

>>> replaceAll "(.*), (.*)" "$2 $1" "Beeblebrox, Zaphod"
"Zaphod Beeblebrox"
>>> replaceAll "4(\\d)" "${1}4" "7458"
"7548"

Dollar signs can be included in the output by doubling them ($$).

>>> replaceAll "(\\d+\\.\\d+)" "$$$1" "9.99"
"$9.99"

Types

data Replace Source

A Replace instance is a function from a regular expression match to a Builder. This naturally forms a Monoid, so they're easy to combine.

Replace also implements IsString, so raw strings can be used to construct them.

High-level interface

replace Source

Arguments

:: Regex

The regular expression to match.

-> Replace

The specification to replace it with.

-> Text

The text to operate on.

-> Text

The text with the first match replaced.

Execute a regular expression on a Text and replace the first match.

replace' Source

Arguments

:: Replace

The specification to replace it with.

-> Match

The match to replace.

-> Text

The text with the match replaced.

Replace one regular expression match with the Replace.

replaceAll Source

Arguments

:: Regex

The regular expression to match.

-> Replace

The specification to replace it with.

-> Text

The text to operate on.

-> Text

The text with all matches replaced.

Execute a regular expression on a Text and replace all matches.

replaceAll' Source

Arguments

:: Replace

The specification to replace it with.

-> [Match]

The matches to replace.

-> Text

The text with all matches replaced.

Replace all regular expression matches with the Replace.

Low-level interface

rgroup Source

Arguments

:: Int

The number of the group in a regular expression.

-> Replace

The Replace that inserts a group's match.

Create a Replace that inserts a regular expression group.

rtext Source

Arguments

:: Text

The static Text to insert.

-> Replace

The Replace that inserts the static Text.

Create a Replace that inserts static Text.

rstring Source

Arguments

:: String

The static String to insert.

-> Replace

The Replace that inserts the static String.

Create a Replace that inserts a static String.

rfn Source

Arguments

:: (Match -> Builder)

The function that creates the replacement text.

-> Replace

The Replace based off that function.

Create a Replace from a function that transforms a Match into a Builder.

rtfn Source

Arguments

:: (Match -> Text)

The function that creates the replacement text.

-> Replace

The Replace based off that function.

Create a Replace From a function that transforms a Match into a Text.

rbuilder Source

Arguments

:: Builder

The Builder to insert.

-> Replace

The Replace that inserts the static Builder.

Create a Replace that inserts a static Builder.