regex-with-pcre-1.1.0.0: Toolkit for regex-base

Safe HaskellNone
LanguageHaskell2010

Text.RE.PCRE.Sequence

Contents

Synopsis

Tutorial

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

The Matches and Match Operators

(*=~) :: Seq Char -> RE -> Matches (Seq Char) Source #

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

countMatches $ s *=~ [re|[0-9]+|]

(?=~) :: Seq Char -> RE -> Match (Seq Char) Source #

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

(*=~/) :: Seq Char -> SearchReplace RE (Seq Char) -> Seq Char Source #

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}|])

(?=~/) :: Seq Char -> SearchReplace RE (Seq Char) -> Seq Char Source #

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

data Matches a #

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

Instances
Functor Matches 
Instance details

Defined in Text.RE.ZeInternals.Types.Matches

Methods

fmap :: (a -> b) -> Matches a -> Matches b #

(<$) :: a -> Matches b -> Matches a #

(RegexContext regex source [MatchText source], RegexLike regex source, RegexFix regex source) => RegexContext regex source (Matches source)

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

Instance details

Defined in Text.RE.ZeInternals.Types.Matches

Methods

match :: regex -> source -> Matches source #

matchM :: MonadFail m => regex -> source -> m (Matches source) #

Eq a => Eq (Matches a) 
Instance details

Defined in Text.RE.ZeInternals.Types.Matches

Methods

(==) :: Matches a -> Matches a -> Bool #

(/=) :: Matches a -> Matches a -> Bool #

Show a => Show (Matches a) 
Instance details

Defined in Text.RE.ZeInternals.Types.Matches

Methods

showsPrec :: Int -> Matches a -> ShowS #

show :: Matches a -> String #

showList :: [Matches a] -> ShowS #

matchesSource :: Matches a -> a #

the source text being matched

allMatches :: Matches a -> [Match a] #

all Match instances found, left to right

anyMatches :: Matches a -> Bool #

tests whether the RE matched the source text at all

countMatches :: Matches a -> Int #

count the matches

matches :: Matches a -> [a] #

list the texts that Matched

The Match Type

data Match a #

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

Instances
Functor Match 
Instance details

Defined in Text.RE.ZeInternals.Types.Match

Methods

fmap :: (a -> b) -> Match a -> Match b #

(<$) :: a -> Match b -> Match a #

(RegexContext regex source (AllTextSubmatches (Array Int) (source, (Int, Int))), RegexLike regex source, RegexFix regex source) => RegexContext regex source (Match source)

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

Instance details

Defined in Text.RE.ZeInternals.Types.Match

Methods

match :: regex -> source -> Match source #

matchM :: MonadFail m => regex -> source -> m (Match source) #

Eq a => Eq (Match a) 
Instance details

Defined in Text.RE.ZeInternals.Types.Match

Methods

(==) :: Match a -> Match a -> Bool #

(/=) :: Match a -> Match a -> Bool #

Show a => Show (Match a) 
Instance details

Defined in Text.RE.ZeInternals.Types.Match

Methods

showsPrec :: Int -> Match a -> ShowS #

show :: Match a -> String #

showList :: [Match a] -> ShowS #

matchSource :: Match a -> a #

the whole source text

matched :: Match a -> Bool #

tests whether the RE matched the source text at all

matchedText :: Match a -> Maybe a #

yields the text matched by the RE, Nothing if no match

The Macros and Parsers

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

data RE Source #

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

Instances
IsOption REOptions Source # 
Instance details

Defined in Text.RE.ZeInternals.PCRE

IsRegex RE String Source # 
Instance details

Defined in Text.RE.PCRE.String

IsRegex RE ByteString Source # 
Instance details

Defined in Text.RE.PCRE.ByteString.Lazy

IsRegex RE ByteString Source # 
Instance details

Defined in Text.RE.PCRE.ByteString

IsRegex RE Text Source # 
Instance details

Defined in Text.RE.PCRE.Text.Lazy

IsRegex RE Text Source # 
Instance details

Defined in Text.RE.PCRE.Text

IsRegex RE (Seq Char) Source # 
Instance details

Defined in Text.RE.PCRE.Sequence

IsOption (Macros RE) Source # 
Instance details

Defined in Text.RE.ZeInternals.PCRE

reSource :: RE -> String Source #

extract the RE source string from the RE

Options

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 apropriate. 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.

data SimpleREOptions #

the default API uses these simple, universal RE options, which get auto-converted into the apropriate 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

Instances
Bounded SimpleREOptions 
Instance details

Defined in Text.RE.REOptions

Enum SimpleREOptions 
Instance details

Defined in Text.RE.REOptions

Eq SimpleREOptions 
Instance details

Defined in Text.RE.REOptions

Ord SimpleREOptions 
Instance details

Defined in Text.RE.REOptions

Show SimpleREOptions 
Instance details

Defined in Text.RE.REOptions

Lift SimpleREOptions

we need to use this in the quasi quoters to specify SimpleREOptions selected by the quasi quoter

Instance details

Defined in Text.RE.REOptions

Methods

lift :: SimpleREOptions -> Q Exp #

IsOption SimpleREOptions Source # 
Instance details

Defined in Text.RE.ZeInternals.PCRE

Compiling and Escaping REs

data SearchReplace re s #

contains a compiled RE and replacement template

Constructors

SearchReplace 

Fields

  • 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 $

Instances
Functor (SearchReplace re) 
Instance details

Defined in Text.RE.ZeInternals.Types.SearchReplace

Methods

fmap :: (a -> b) -> SearchReplace re a -> SearchReplace re b #

(<$) :: a -> SearchReplace re b -> SearchReplace re a #

(Show re, Show s) => Show (SearchReplace re s) 
Instance details

Defined in Text.RE.ZeInternals.Types.SearchReplace

Methods

showsPrec :: Int -> SearchReplace re s -> ShowS #

show :: SearchReplace re s -> String #

showList :: [SearchReplace re s] -> ShowS #

compileRegex :: (Functor m, Monad m, MonadFail m) => String -> m RE Source #

compile a String into a RE with the default options, generating an error if the RE is not well formed

compileRegexWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> String -> m RE Source #

compile a String into a RE using the given SimpleREOptions, generating an error if the RE is not well formed

compileSearchReplace :: (Monad m, MonadFail m, Functor m, IsRegex RE s) => String -> String -> m (SearchReplace RE s) Source #

compile a SearchReplace template generating errors if the RE or the template are not well formed, all capture references being checked

compileSearchReplaceWith :: (Monad m, MonadFail m, Functor m, IsRegex RE s) => SimpleREOptions -> String -> String -> m (SearchReplace RE s) Source #

compile a SearchReplace template, with simple options, generating errors if the RE or the template are not well formed, all capture references being checked

escape :: (Functor m, Monad m, MonadFail m) => (String -> String) -> String -> m RE Source #

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 (("^"++) . (++"$"))

escapeWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> (String -> String) -> String -> m RE Source #

a variant of escape where the SimpleREOptions are specified

escapeREString :: String -> String #

Convert a string into a regular expression that will match that string

The Classic regex-base Match Operators

(=~) :: (Typeable a, RegexContext Regex (Seq Char) a) => Seq Char -> RE -> a Source #

the `regex-base` polymorphic match operator

(=~~) :: (Monad m, MonadFail m, Functor m, Typeable a, RegexContext Regex (Seq Char) a) => Seq Char -> RE -> m a Source #

the `regex-base` monadic, polymorphic match operator

The re Quasi Quoters

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

re :: QuasiQuoter Source #

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

reMultilineSensitive :: QuasiQuoter Source #

[reMultilineSensitive| ... |], compiles a case-sensitive, multi-line RE

reMultilineInsensitive :: QuasiQuoter Source #

[reMultilineInsensitive| ... |], compiles a case-insensitive, multi-line RE

reBlockSensitive :: QuasiQuoter Source #

[reMultilineInsensitive| ... |], compiles a case-sensitive, non-multi-line RE

reBlockInsensitive :: QuasiQuoter Source #

[reMultilineInsensitive| ... |], compiles a case-insensitive, non-multi-line RE

reMS :: QuasiQuoter Source #

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

reMI :: QuasiQuoter Source #

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

reBS :: QuasiQuoter Source #

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

reBI :: QuasiQuoter Source #

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

re_ :: QuasiQuoter Source #

[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 upper- or lower-case hex didgits.

The Ed Quasi Quoters

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

edMultilineSensitive :: QuasiQuoter Source #

[edMultilineSensitive| ... /// ... |] compiles a case-sensitive, multi-line SearchReplace template

edMultilineInsensitive :: QuasiQuoter Source #

[edMultilineInsensitive| ... /// ... |] compiles a case-insensitive, multi-line SearchReplace template

edBlockSensitive :: QuasiQuoter Source #

[edBlockSensitive| ... /// ... |] compiles a case-sensitive, non-multi-line SearchReplace template

edBlockInsensitive :: QuasiQuoter Source #

[edBlockInsensitive| ... /// ... |] compiles a case-insensitive, non-multi-line SearchReplace template

ed :: QuasiQuoter Source #

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

edMS :: QuasiQuoter Source #

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

edMI :: QuasiQuoter Source #

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

edBS :: QuasiQuoter Source #

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

edBI :: QuasiQuoter Source #

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

ed_ :: QuasiQuoter Source #

[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 didgits (which is entirely equivalent in this example to just using [edMultilineInsensitive|[0-9a-f]+|]).

The cp Quasi Quoters

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

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

Orphan instances