HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

  • Packageregex-1.1.0.2
  • Exports48
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceText.hs

Tutorial

0 declarations

We have a regex tutorial at http://tutorial.regex.uk.

The Matches and Match Operators

2 declarations
value(*=~) :: Text -> RE -> Matches Text
#

find all the matches in the argument text; e.g., to count the number of naturals in s:

countMatches $ s *=~ [re|[0-9]+|]
value(?=~) :: Text -> RE -> Match Text
#

find the first match in the argument text; e.g., to test if there is a natural number in the input text:

matched $ s ?=~ [re|[0-9]+|]

The SearchReplace Operators

2 declarations
value(*=~/) :: Text -> SearchReplace RE Text -> Text
#

search and replace all matches in the argument text; e.g., this section will convert every YYYY-MM-DD format date in its argument text into a DD/MM/YYYY date:

(*=~/ [ed|${y}([0-9]{4})-0*${m}([0-9]{2})-0*${d}([0-9]{2})///${d}/${m}/${y}|])
value(?=~/) :: Text -> SearchReplace RE Text -> Text
#

search and replace the first occurrence only (if any) in the input text e.g., to prefix the first string of four hex digits in the input text, if any, with 0x:

(?=~/ [ed|[0-9A-Fa-f]{4}///0x$0|])

The Matches Type

6 declarations
datadata Matches a
#

the result of matching a RE against a text (with *=~), retaining the text that was matched against

Instances4Functor, RegexContext, Eq, Show
  • Functor MatchesDefined in regex-1.1.0.2 · Text.RE.ZeInternals.Types.Matches
  • (RegexContext regex source [MatchText source], RegexLike regex source, RegexFix regex source) => RegexContext regex source (Matches source)Defined in regex-1.1.0.2 · Text.RE.ZeInternals.Types.Matches

    this instance hooks Matches into regex-base: regex consumers need not worry about any of this

  • Eq a => Eq (Matches a)Defined in regex-1.1.0.2 · Text.RE.ZeInternals.Types.Matches
  • Show a => Show (Matches a)Defined in regex-1.1.0.2 · Text.RE.ZeInternals.Types.Matches

The Match Type

4 declarations
datadata Match a
#

the result of matching a RE to a text once (with ?=~), retaining the text that was matched against

Instances4Functor, RegexContext, Eq, Show
  • Functor MatchDefined in regex-1.1.0.2 · Text.RE.ZeInternals.Types.Match
  • (RegexContext regex source (AllTextSubmatches (Array Int) (source, (Int, Int))), RegexLike regex source, RegexFix regex source) => RegexContext regex source (Match source)Defined in regex-1.1.0.2 · Text.RE.ZeInternals.Types.Match

    this instance hooks Match into regex-base: regex consumers need not worry about any of this

  • Eq a => Eq (Match a)Defined in regex-1.1.0.2 · Text.RE.ZeInternals.Types.Match
  • Show a => Show (Match a)Defined in regex-1.1.0.2 · Text.RE.ZeInternals.Types.Match
valuematched :: Match a -> Bool
#

tests whether the RE matched the source text at all

The Macros and Parsers

0 declarations

There are a number of RE macros and corresponding Haskell parsers for parsing the matched text into appropriate Haskell types. See the Macros Tables for details.

The RE Type

2 declarations
datadata RE
#

the RE type for this back end representing a well-formed, compiled RE

Instances8IsRegex, IsOption, …

Options

1 declaration

You can specify different compilation options by appending a to the name of an [re| ... |] or [ed| ... /// ... |] quasi quoter to select the corresponding compilation option. For example, the section,

(?=~/ [edBlockInsensitive|foo$///bar|])

will replace a foo suffix of the argument text, of any capitalisation, with a (lower case) bar. If you need to specify the options dynamically, use the [re_| ... |] and [ed_| ... /// ... |] quasi quoters, which generate functions that take an IsOption option (e.g., a SimpleReOptions value) and yields a RE or SearchReplace as appropriate. For example if you have a SimpleReOptions value in sro then

(?=~/ [ed_|foo$///bar|] sro)

will compile the foo$ RE according to the value of sro. For more on specifying RE options see Text.RE.REOptions.

datadata SimpleREOptions
#

the default API uses these simple, universal RE options, which get auto-converted into the appropriate back-end REOptions_

Constructors

  • MultilineSensitive

    case-sensitive with ^ and $ matching the start and end of a line

  • MultilineInsensitive

    case-insensitive with ^ and $ matsh the start and end of a line

  • BlockSensitive

    case-sensitive with ^ and $ matching the start and end of the input text

  • BlockInsensitive

    case-insensitive with ^ and $ matching the start and end of the input text

Instances7Bounded, Enum, Eq, Ord, Show, IsOption, …

Compiling and Escaping REs

8 declarations
datadata SearchReplace re s
#

contains a compiled RE and replacement template

Constructors

  • SearchReplace
    • getSearch :: !re

      the RE to match a string to replace

    • getTemplate :: !s

      the replacement template with ${cap} used to identify a capture (by number or name if one was given) and $$ being used to escape a single $

Instances2Functor, Show
valueescape
  1. :: (Functor m, Monad m, MonadFail m)
  2. => String -> String
  3. -> String
  4. -> m RE
#

convert a string into a RE that matches that string, and apply it to an argument continuation function to make up the RE string to be compiled; e.g., to compile a RE that will only match the string:

maybe undefined id . escape (("^"++) . (++"$"))

The Classic regex-base Match Operators

2 declarations

The re Quasi Quoters

10 declarations

The [re|.*|] quasi quoters, with variants for specifying different options to the RE compiler (see Text.RE.REOptions), and the specialised back-end types and functions.

valuere :: QuasiQuoter
#

[re| ... |], is equivalent to [reMultilineSensitive| ... |], compiling a case-sensitive, multi-line RE

valuereMS :: QuasiQuoter
#

[reMS| ... |] is a shorthand for [reMultilineSensitive| ... |]

valuereMI :: QuasiQuoter
#

[reMI| ... |] is a shorthand for [reMultilineInsensitive| ... |]

valuereBS :: QuasiQuoter
#

[reBS| ... |] is a shorthand for [reBlockSensitive| ... |]

valuereBI :: QuasiQuoter
#

[reBI| ... |] is a shorthand for [reBlockInsensitive| ... |]

valuere_ :: QuasiQuoter
#

[re_| ... |] compiles a RE to produce a function that takes the RE options (e.g., a SimpleREOptions value) and yields the RE compiled with those options. For example,

countMatches $ s *=~ [re_|[0-9a-f]+|] MultilineInsensitive

counts the number of hexadecimal digit strings in s, allowing for upper- or lower-case hex digits (which is entirely equivalent in this example to just using [reMultilineInsensitive|[0-9a-f]+|]).

The Ed Quasi Quoters

10 declarations

The [ed|.*///foo|] quasi quoters, with variants for specifying different options to the RE compiler (see Text.RE.REOptions).

valueed :: QuasiQuoter
#

[ed| ... /// ... |], is equivalent to [edMultilineSensitive| ... /// ... |], compiling a case-sensitive, multi-line SearchReplace

valueedMS :: QuasiQuoter
#

[edMS| ... /// ... |] is a shorthand for [edMultilineSensitive| ... /// ... |]

valueedMI :: QuasiQuoter
#

[edMI| ... /// ... |] is a shorthand for [edMultilineInsensitive| ... /// ... |]

valueedBS :: QuasiQuoter
#

[edBS| ... /// ... |] is a shorthand for [edBlockSensitive| ... /// ... |]

valueedBI :: QuasiQuoter
#

[edBI| ... /// ... |] is a shorthand for [edBlockInsensitive| ... /// ... |]

valueed_ :: QuasiQuoter
#

[ed_| ... /// ... |] compiles a SearchReplace template to produce a function that takes the RE options (e.g., a SimpleREOptions value) and yields the SearchReplace template compiled with those options. For example,

s *=~/ [ed_|${hex}([0-9a-f]+)///0x${hex}|] MultilineInsensitive

prefixes the hexadecimal digit strings in s with 0x, allowing for upper- or lower-case hex digits (which is entirely equivalent in this example to just using [edMultilineInsensitive|[0-9a-f]+|]).

The cp Quasi Quoters

1 declaration
valuecp :: QuasiQuoter
#

quasi quoter for CaptureID: [cp|0|], [cp|0|], etc., indexing captures by classic positional numbers, and [cp|foo|], etc., referencing a named capture [re| ... ${foo}( ... ) ... |].

IsRegex

0 declarations

The IsRegex class is used to abstract over the different regex back ends and the text types they work with -- see Text.RE.Tools.IsRegex for details.

Orphan instances

1 instance