regex-base-0.93.1: Replaces/Enhances Text.Regex

Portabilitynon-portable (MPTC+FD)
Stabilityexperimental
Maintainerlibraries@haskell.org, textregexlazy@personal.mightyreason.com

Text.Regex.Base.RegexLike

Contents

Description

Classes and instances for Regex matching.

All the classes are declared here, and some common type aliases, and the MatchResult data type.

The only instances here are for Extract String and Extract ByteString. There are no data values. The RegexContext instances are in Text.Regex.Base.Context, except for ones which run afoul of a repeated variable (RegexContext regex a a), which are defined in each modules' String and ByteString modules.

Synopsis

Type aliases

type MatchOffset = IntSource

0 based index from start of source, or (-1) for unused

type MatchLength = IntSource

non-negative length of a match

type MatchArray = Array Int (MatchOffset, MatchLength)Source

0 based array, with 0th index indicating the full match. If the full match location is not available, represent as (0,0).

type MatchText source = Array Int (source, (MatchOffset, MatchLength))Source

Data types

data MatchResult a Source

This is the same as the type from JRegex.

Constructors

MR 

Fields

mrBefore :: a
 
mrMatch :: a
 
mrAfter :: a
 
mrSubList :: [a]
 
mrSubs :: Array Int a
 

Instances

Classes

class RegexOptions regex compOpt execOpt | regex -> compOpt execOpt, compOpt -> regex execOpt, execOpt -> regex compOpt whereSource

Rather than carry them around spearately, the options for how to execute a regex are kept as part of the regex. There are two types of options. Those that can only be specified at compilation time and never changed are CompOpt. Those that can be changed later and affect how matching is performed are ExecOpt. The actually types for these depend on the backend.

Methods

blankCompOptSource

Arguments

:: compOpt

no options set at all in the backend

blankExecOptSource

Arguments

:: execOpt

no options set at all in the backend

defaultCompOptSource

Arguments

:: compOpt

reasonable options (extended,caseSensitive,multiline regex)

defaultExecOptSource

Arguments

:: execOpt

reasonable options (extended,caseSensitive,multiline regex)

setExecOpts :: execOpt -> regex -> regexSource

forget old flags and use new ones

getExecOpts :: regex -> execOptSource

retrieve the current flags

class RegexOptions regex compOpt execOpt => RegexMaker regex compOpt execOpt source | regex -> compOpt execOpt, compOpt -> regex execOpt, execOpt -> regex compOpt whereSource

RegexMaker captures the creation of the compiled regular expression from a source type and an option type. makeRegexM and makeRegexM report parse error using MonadError, usually (Either String regex).

The makeRegex function has a default implementation that depends on makeRegexOpts and used defaultCompOpt and defaultExecOpt. Similarly for makeRegexM and makeRegexOptsM.

There are also default implementaions for makeRegexOpts and makeRegexOptsM in terms of each other. So a minimal instance definition needs to only define one of these, hopefully makeRegexOptsM.

Methods

makeRegex :: source -> regexSource

make using the defaultCompOpt and defaultExecOpt

makeRegexOpts :: compOpt -> execOpt -> source -> regexSource

Specify your own options

makeRegexM :: Monad m => source -> m regexSource

make using the defaultCompOpt and defaultExecOpt, reporting errors with fail

makeRegexOptsM :: Monad m => compOpt -> execOpt -> source -> m regexSource

Specify your own options, reporting errors with fail

class Extract source => RegexLike regex source whereSource

RegexLike is parametrized on a regular expression type and a source type to run the matching on.

There are default implementations: matchTest and matchOnceText use matchOnce; matchCount and matchAllText use matchAll. matchOnce uses matchOnceText and matchAll uses matchAllText. So a minimal complete instance need to provide at least (matchOnce or matchOnceText) and (matchAll or matchAllText). Additional definitions are often provided where they will increase efficiency.

 [ c | let notVowel = makeRegex "[^aeiou]" :: Regex, c <- ['a'..'z'], matchTest notVowel [c]  ]

 "bcdfghjklmnpqrstvwxyz"

The strictness of these functions is instance dependent.

Methods

matchOnce :: regex -> source -> Maybe MatchArraySource

This returns the first match in the source (it checks the whole source, not just at the start). This returns an array of (offset,length) index pairs for the match and captured substrings. The offset is 0-based. A (-1) for an offset means a failure to match. The lower bound of the array is 0, and the 0th element is the (offset,length) for the whole match.

matchAll :: regex -> source -> [MatchArray]Source

matchAll returns a list of matches. The matches are in order and do not overlap. If any match succeeds but has 0 length then this will be the last match in the list.

matchCount :: regex -> source -> IntSource

matchCount returns the number of non-overlapping matches returned by matchAll.

matchTest :: regex -> source -> BoolSource

matchTest return True if there is a match somewhere in the source (it checks the whole source not just at the start).

matchAllText :: regex -> source -> [MatchText source]Source

This is matchAll with the actual subsections of the source instead of just the (offset,length) information.

matchOnceText :: regex -> source -> Maybe (source, MatchText source, source)Source

This can return a tuple of three items: the source before the match, an array of the match and captured substrings (with their indices), and the source after the match.

class RegexLike regex source => RegexContext regex source target whereSource

RegexContext is the polymorphic interface to do matching. Since target is polymorphic you may need to suply the type explicitly in contexts where it cannot be inferred.

The monadic matchM version uses fail to report when the regex has no match in source. Two examples:

Here the contest Bool is inferred:

 [ c | let notVowel = makeRegex "[^aeiou]" :: Regex, c <- ['a'..'z'], match notVowel [c]  ]

 "bcdfghjklmnpqrstvwxyz"

Here the context '[String]' must be supplied:

 let notVowel = (makeRegex "[^aeiou]" :: Regex )
 in do { c <- ['a'..'z'] ; matchM notVowel [c] } :: [String]

 ["b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"]

Methods

match :: regex -> source -> targetSource

matchM :: Monad m => regex -> source -> m targetSource

Instances

RegexLike a b => RegexContext a b MatchArray 
RegexLike a b => RegexContext a b Int 
RegexLike a b => RegexContext a b () 
RegexLike a b => RegexContext a b Bool 
RegexLike a b => RegexContext a b [[b]] 
RegexLike a b => RegexContext a b [MatchText b] 
RegexLike a b => RegexContext a b [MatchArray] 
RegexLike a b => RegexContext a b (MatchResult b) 
RegexLike a b => RegexContext a b (AllTextMatches (Array Int) (Array Int b)) 
RegexLike a b => RegexContext a b (AllTextMatches [] (Array Int b)) 
RegexLike a b => RegexContext a b (AllTextMatches (Array Int) [b]) 
RegexLike a b => RegexContext a b (AllTextMatches (Array Int) b) 
RegexLike a b => RegexContext a b (AllTextMatches [] b) 
RegexLike a b => RegexContext a b (AllTextMatches (Array Int) (MatchText b)) 
RegexLike a b => RegexContext a b (AllMatches (Array Int) MatchArray) 
RegexLike a b => RegexContext a b (AllMatches (Array Int) (MatchOffset, MatchLength)) 
RegexLike a b => RegexContext a b (AllMatches [] (MatchOffset, MatchLength)) 
RegexLike a b => RegexContext a b (AllTextSubmatches (Array Int) b) 
RegexLike a b => RegexContext a b (AllTextSubmatches [] b) 
RegexLike a b => RegexContext a b (AllTextSubmatches [] (b, (MatchOffset, MatchLength))) 
RegexLike a b => RegexContext a b (AllTextSubmatches (Array Int) (b, (MatchOffset, MatchLength))) 
RegexLike a b => RegexContext a b (AllSubmatches [] (MatchOffset, MatchLength)) 
RegexLike a b => RegexContext a b (MatchOffset, MatchLength) 
RegexLike a b => RegexContext a b (b, b, b) 
RegexLike a b => RegexContext a b (b, MatchText b, b) 
RegexLike a b => RegexContext a b (b, b, b, [b]) 

class Extract source whereSource

Extract allows for indexing operations on String or ByteString.

Methods

before :: Int -> source -> sourceSource

before is a renamed take

after :: Int -> source -> sourceSource

after is a renamed drop

empty :: sourceSource

For when there is no match, this can construct an empty data value

extract :: (Int, Int) -> source -> sourceSource

extract takes an offset and length and has a default implementation of extract (off,len) source = before len (after off source)

newtype AllSubmatches f b Source

Used in results of RegexContext instances

Constructors

AllSubmatches 

Fields

getAllSubmatches :: f b
 

newtype AllTextSubmatches f b Source

Used in results of RegexContext instances

Constructors

AllTextSubmatches 

Fields

getAllTextSubmatches :: f b
 

newtype AllMatches f b Source

Used in results of RegexContext instances

Constructors

AllMatches 

Fields

getAllMatches :: f b
 

newtype AllTextMatches f b Source

Used in results of RegexContext instances

Constructors

AllTextMatches 

Fields

getAllTextMatches :: f b