regex-base-0.94.0.3: Common "Text.Regex.*" API for Regex matching

Copyright | (c) Chris Kuklewicz 2006
License | BSD-3-Clause
Maintainer | Andreas Abel
Stability | stable
Portability | non-portable (MPTC+FD)
Safe Haskell | Safe
Language | Haskell2010

Text.Regex.Base.RegexLike

Contents

- Type aliases
- Data types
- Classes

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`, `Extract` `ByteString`, and `Extract` `Text`. 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 MatchOffset = Int
- type MatchLength = Int
- type MatchArray = Array Int (MatchOffset, MatchLength)
- type MatchText source = Array Int (source, (MatchOffset, MatchLength))
- data MatchResult a = MR {
  - mrBefore :: a
  - mrMatch :: a
  - mrAfter :: a
  - mrSubList :: [a]
  - mrSubs :: Array Int a
  }
- class RegexOptions regex compOpt execOpt | regex -> compOpt execOpt, compOpt -> regex execOpt, execOpt -> regex compOpt where
  - blankCompOpt :: compOpt
  - blankExecOpt :: execOpt
  - defaultCompOpt :: compOpt
  - defaultExecOpt :: execOpt
  - setExecOpts :: execOpt -> regex -> regex
  - getExecOpts :: regex -> execOpt
- class RegexOptions regex compOpt execOpt => RegexMaker regex compOpt execOpt source | regex -> compOpt execOpt, compOpt -> regex execOpt, execOpt -> regex compOpt where
  - makeRegex :: source -> regex
  - makeRegexOpts :: compOpt -> execOpt -> source -> regex
  - makeRegexM :: MonadFail m => source -> m regex
  - makeRegexOptsM :: MonadFail m => compOpt -> execOpt -> source -> m regex
- class Extract source => RegexLike regex source where
  - matchOnce :: regex -> source -> Maybe MatchArray
  - matchAll :: regex -> source -> [MatchArray]
  - matchCount :: regex -> source -> Int
  - matchTest :: regex -> source -> Bool
  - matchAllText :: regex -> source -> [MatchText source]
  - matchOnceText :: regex -> source -> Maybe (source, MatchText source, source)
- class RegexLike regex source => RegexContext regex source target where
  - match :: regex -> source -> target
  - matchM :: MonadFail m => regex -> source -> m target
- class Extract source where
  - before :: Int -> source -> source
  - after :: Int -> source -> source
  - empty :: source
  - extract :: (Int, Int) -> source -> source
- newtype AllSubmatches (f :: Type -> Type) b = AllSubmatches {
  - getAllSubmatches :: f b
  }
- newtype AllTextSubmatches (f :: Type -> Type) b = AllTextSubmatches {
  - getAllTextSubmatches :: f b
  }
- newtype AllMatches (f :: Type -> Type) b = AllMatches {
  - getAllMatches :: f b
  }
- newtype AllTextMatches (f :: Type -> Type) b = AllTextMatches {
  - getAllTextMatches :: f b
  }

## Type aliases

type MatchOffset = Int Source #

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

type MatchLength = Int Source #

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

Instances details

RegexLike a b => RegexContext a b (MatchResult b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> MatchResult b Source # matchM :: MonadFail m => a -> b -> m (MatchResult b) Source #

## Classes

class RegexOptions regex compOpt execOpt | regex -> compOpt execOpt, compOpt -> regex execOpt, execOpt -> regex compOpt where Source #

Rather than carry them around separately, 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

blankCompOpt :: compOpt Source #

No options set at all in the backend.

blankExecOpt :: execOpt Source #

No options set at all in the backend.

defaultCompOpt :: compOpt Source #

Reasonable options (extended, caseSensitive, multiline regex).

defaultExecOpt :: execOpt Source #

Reasonable options (extended, caseSensitive, multiline regex).

setExecOpts :: execOpt -> regex -> regex Source #

Forget old flags and use new ones.

getExecOpts :: regex -> execOpt Source #

Retrieve the current flags.

class RegexOptions regex compOpt execOpt => RegexMaker regex compOpt execOpt source | regex -> compOpt execOpt, compOpt -> regex execOpt, execOpt -> regex compOpt where Source #

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

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

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

Minimal complete definition

Nothing

Methods

makeRegex :: source -> regex Source #

Use the `defaultCompOpt` and `defaultExecOpt`.

makeRegexOpts :: compOpt -> execOpt -> source -> regex Source #

Specify your own options.

makeRegexM :: MonadFail m => source -> m regex Source #

Use the `defaultCompOpt` and `defaultExecOpt`, reporting errors with `fail`.

makeRegexOptsM :: MonadFail m => compOpt -> execOpt -> source -> m regex Source #

Specify your own options, reporting errors with fail

class Extract source => RegexLike regex source where Source #

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`. Conversely, `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.

Minimal complete definition

Nothing

Methods

matchOnce :: regex -> source -> Maybe MatchArray Source #

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 -> Int Source #

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

matchTest :: regex -> source -> Bool Source #

`matchTest` returns `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 where Source #

`RegexContext` is the polymorphic interface to do matching. Since `target` is polymorphic you may need to supply 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 -> target Source #

matchM :: MonadFail m => regex -> source -> m target Source #

#### Instances

Instances details

RegexLike a b => RegexContext a b MatchArray Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> MatchArray Source # matchM :: MonadFail m => a -> b -> m MatchArray Source #
RegexLike a b => RegexContext a b () Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> () Source # matchM :: MonadFail m => a -> b -> m () Source #
RegexLike a b => RegexContext a b Bool Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> Bool Source # matchM :: MonadFail m => a -> b -> m Bool Source #
RegexLike a b => RegexContext a b Int Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> Int Source # matchM :: MonadFail m => a -> b -> m Int Source #
RegexLike a b => RegexContext a b (MatchResult b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> MatchResult b Source # matchM :: MonadFail m => a -> b -> m (MatchResult b) Source #
RegexLike a b => RegexContext a b [MatchArray] Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> [MatchArray] Source # matchM :: MonadFail m => a -> b -> m [MatchArray] Source #
RegexLike a b => RegexContext a b [MatchText b] Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> [MatchText b] Source # matchM :: MonadFail m => a -> b -> m [MatchText b] Source #
RegexLike a b => RegexContext a b [[b]] Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> [[b]] Source # matchM :: MonadFail m => a -> b -> m [[b]] Source #
RegexLike a b => RegexContext a b (AllMatches (Array Int) MatchArray) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllMatches (Array Int) MatchArray Source # matchM :: MonadFail m => a -> b -> m (AllMatches (Array Int) MatchArray) Source #
RegexLike a b => RegexContext a b (AllMatches (Array Int) (MatchOffset, MatchLength)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllMatches (Array Int) (MatchOffset, MatchLength) Source # matchM :: MonadFail m => a -> b -> m (AllMatches (Array Int) (MatchOffset, MatchLength)) Source #
RegexLike a b => RegexContext a b (AllMatches [] (MatchOffset, MatchLength)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllMatches [] (MatchOffset, MatchLength) Source # matchM :: MonadFail m => a -> b -> m (AllMatches [] (MatchOffset, MatchLength)) Source #
RegexLike a b => RegexContext a b (AllSubmatches [] (MatchOffset, MatchLength)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllSubmatches [] (MatchOffset, MatchLength) Source # matchM :: MonadFail m => a -> b -> m (AllSubmatches [] (MatchOffset, MatchLength)) Source #
RegexLike a b => RegexContext a b (AllTextMatches (Array Int) (Array Int b)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches (Array Int) (Array Int b) Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches (Array Int) (Array Int b)) Source #
RegexLike a b => RegexContext a b (AllTextMatches (Array Int) (MatchText b)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches (Array Int) (MatchText b) Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches (Array Int) (MatchText b)) Source #
RegexLike a b => RegexContext a b (AllTextMatches (Array Int) [b]) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches (Array Int) [b] Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches (Array Int) [b]) Source #
RegexLike a b => RegexContext a b (AllTextMatches (Array Int) b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches (Array Int) b Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches (Array Int) b) Source #
RegexLike a b => RegexContext a b (AllTextMatches [] (Array Int b)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches [] (Array Int b) Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches [] (Array Int b)) Source #
RegexLike a b => RegexContext a b (AllTextMatches [] b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches [] b Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches [] b) Source #
RegexLike a b => RegexContext a b (AllTextSubmatches (Array Int) (b, (MatchOffset, MatchLength))) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextSubmatches (Array Int) (b, (MatchOffset, MatchLength)) Source # matchM :: MonadFail m => a -> b -> m (AllTextSubmatches (Array Int) (b, (MatchOffset, MatchLength))) Source #
RegexLike a b => RegexContext a b (AllTextSubmatches (Array Int) b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextSubmatches (Array Int) b Source # matchM :: MonadFail m => a -> b -> m (AllTextSubmatches (Array Int) b) Source #
RegexLike a b => RegexContext a b (AllTextSubmatches [] (b, (MatchOffset, MatchLength))) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextSubmatches [] (b, (MatchOffset, MatchLength)) Source # matchM :: MonadFail m => a -> b -> m (AllTextSubmatches [] (b, (MatchOffset, MatchLength))) Source #
RegexLike a b => RegexContext a b (AllTextSubmatches [] b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextSubmatches [] b Source # matchM :: MonadFail m => a -> b -> m (AllTextSubmatches [] b) Source #
RegexLike a b => RegexContext a b (MatchOffset, MatchLength) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> (MatchOffset, MatchLength) Source # matchM :: MonadFail m => a -> b -> m (MatchOffset, MatchLength) Source #
RegexLike a b => RegexContext a b (b, MatchText b, b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> (b, MatchText b, b) Source # matchM :: MonadFail m => a -> b -> m (b, MatchText b, b) Source #
RegexLike a b => RegexContext a b (b, b, b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> (b, b, b) Source # matchM :: MonadFail m => a -> b -> m (b, b, b) Source #
RegexLike a b => RegexContext a b (b, b, b, [b]) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> (b, b, b, [b]) Source # matchM :: MonadFail m => a -> b -> m (b, b, b, [b]) Source #

class Extract source where Source #

Extract allows for indexing operations on `String` or `ByteString`.

Minimal complete definition

before, after, empty

Methods

before :: Int -> source -> source Source #

`before` is a renamed `take`.

after :: Int -> source -> source Source #

`after` is a renamed `drop`.

empty :: source Source #

When there is no match, this can construct an empty data value.

extract :: (Int, Int) -> source -> source Source #

`extract` takes an offset and length, and has this default implementation:

      extract (off, len) source = before len (after off source)

#### Instances

Instances details

Extract ByteString Source # |
Instance details Defined in Text.Regex.Base.RegexLike Methods before :: Int -> ByteString -> ByteString Source # after :: Int -> ByteString -> ByteString Source # empty :: ByteString Source # extract :: (Int, Int) -> ByteString -> ByteString Source #
Extract ByteString Source # |
Instance details Defined in Text.Regex.Base.RegexLike Methods before :: Int -> ByteString -> ByteString Source # after :: Int -> ByteString -> ByteString Source # empty :: ByteString Source # extract :: (Int, Int) -> ByteString -> ByteString Source #
Extract Text Source # | Since: 0.94.0.0
Instance details Defined in Text.Regex.Base.RegexLike Methods before :: Int -> Text -> Text Source # after :: Int -> Text -> Text Source # empty :: Text Source # extract :: (Int, Int) -> Text -> Text Source #
Extract Text Source # | Since: 0.94.0.0
Instance details Defined in Text.Regex.Base.RegexLike Methods before :: Int -> Text -> Text Source # after :: Int -> Text -> Text Source # empty :: Text Source # extract :: (Int, Int) -> Text -> Text Source #
Extract String Source # |
Instance details Defined in Text.Regex.Base.RegexLike Methods before :: Int -> String -> String Source # after :: Int -> String -> String Source # empty :: String Source # extract :: (Int, Int) -> String -> String Source #
Extract (Seq a) Source # |
Instance details Defined in Text.Regex.Base.RegexLike Methods before :: Int -> Seq a -> Seq a Source # after :: Int -> Seq a -> Seq a Source # empty :: Seq a Source # extract :: (Int, Int) -> Seq a -> Seq a Source #

newtype AllSubmatches (f :: Type -> Type) b Source #

Used in results of `RegexContext` instances.

Constructors

AllSubmatches |

Fields

- getAllSubmatches :: f b

#### Instances

Instances details

RegexLike a b => RegexContext a b (AllSubmatches [] (MatchOffset, MatchLength)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllSubmatches [] (MatchOffset, MatchLength) Source # matchM :: MonadFail m => a -> b -> m (AllSubmatches [] (MatchOffset, MatchLength)) Source #

newtype AllTextSubmatches (f :: Type -> Type) b Source #

Used in results of `RegexContext` instances.

Constructors

AllTextSubmatches |

Fields

- getAllTextSubmatches :: f b

#### Instances

Instances details

RegexLike a b => RegexContext a b (AllTextSubmatches (Array Int) (b, (MatchOffset, MatchLength))) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextSubmatches (Array Int) (b, (MatchOffset, MatchLength)) Source # matchM :: MonadFail m => a -> b -> m (AllTextSubmatches (Array Int) (b, (MatchOffset, MatchLength))) Source #
RegexLike a b => RegexContext a b (AllTextSubmatches (Array Int) b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextSubmatches (Array Int) b Source # matchM :: MonadFail m => a -> b -> m (AllTextSubmatches (Array Int) b) Source #
RegexLike a b => RegexContext a b (AllTextSubmatches [] (b, (MatchOffset, MatchLength))) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextSubmatches [] (b, (MatchOffset, MatchLength)) Source # matchM :: MonadFail m => a -> b -> m (AllTextSubmatches [] (b, (MatchOffset, MatchLength))) Source #
RegexLike a b => RegexContext a b (AllTextSubmatches [] b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextSubmatches [] b Source # matchM :: MonadFail m => a -> b -> m (AllTextSubmatches [] b) Source #

newtype AllMatches (f :: Type -> Type) b Source #

Used in results of `RegexContext` instances.

Constructors

AllMatches |

Fields

- getAllMatches :: f b

#### Instances

Instances details

RegexLike a b => RegexContext a b (AllMatches (Array Int) MatchArray) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllMatches (Array Int) MatchArray Source # matchM :: MonadFail m => a -> b -> m (AllMatches (Array Int) MatchArray) Source #
RegexLike a b => RegexContext a b (AllMatches (Array Int) (MatchOffset, MatchLength)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllMatches (Array Int) (MatchOffset, MatchLength) Source # matchM :: MonadFail m => a -> b -> m (AllMatches (Array Int) (MatchOffset, MatchLength)) Source #
RegexLike a b => RegexContext a b (AllMatches [] (MatchOffset, MatchLength)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllMatches [] (MatchOffset, MatchLength) Source # matchM :: MonadFail m => a -> b -> m (AllMatches [] (MatchOffset, MatchLength)) Source #

newtype AllTextMatches (f :: Type -> Type) b Source #

Used in results of `RegexContext` instances.

Constructors

AllTextMatches |

Fields

- getAllTextMatches :: f b

#### Instances

Instances details

RegexLike a b => RegexContext a b (AllTextMatches (Array Int) (Array Int b)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches (Array Int) (Array Int b) Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches (Array Int) (Array Int b)) Source #
RegexLike a b => RegexContext a b (AllTextMatches (Array Int) (MatchText b)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches (Array Int) (MatchText b) Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches (Array Int) (MatchText b)) Source #
RegexLike a b => RegexContext a b (AllTextMatches (Array Int) [b]) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches (Array Int) [b] Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches (Array Int) [b]) Source #
RegexLike a b => RegexContext a b (AllTextMatches (Array Int) b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches (Array Int) b Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches (Array Int) b) Source #
RegexLike a b => RegexContext a b (AllTextMatches [] (Array Int b)) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches [] (Array Int b) Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches [] (Array Int b)) Source #
RegexLike a b => RegexContext a b (AllTextMatches [] b) Source # |
Instance details Defined in Text.Regex.Base.Context Methods match :: a -> b -> AllTextMatches [] b Source # matchM :: MonadFail m => a -> b -> m (AllTextMatches [] b) Source #