hledger-lib-1.52.1: A library providing the core functionality of hledger

Safe Haskell | None
Language | GHC2021

Hledger.Utils.Regex

Contents

- Regexp type and constructors
- type aliases
- total regex operations

Description

Easy regular expression helpers, currently based on regex-tdfa. These should:

- be cross-platform, not requiring C libraries
- support unicode
- support extended regular expressions
- support replacement, with backreferences etc.
- support splitting
- have mnemonic names
- have simple monomorphic types
- work with simple strings

Regex strings are automatically compiled into regular expressions the first time they are seen, and these are cached. If you use a huge number of unique regular expressions this might lead to increased memory usage. Several functions have memoised variants (*Memo), which also trade space for time.

Currently two APIs are provided:

- The old partial one (with ' suffixes') which will call error on any problem (eg with malformed regexps). This comes from hledger's origin as a command-line tool.
- The new total one which will return an error message. This is better for long-running apps like hledger-web.

Current limitations:

- (?i) and similar are not supported

Synopsis

- data Regexp
- toRegex :: Text -> Either RegexError Regexp
- toRegexCI :: Text -> Either RegexError Regexp
- toRegex' :: Text -> Regexp
- toRegexCI' :: Text -> Regexp
- type Replacement = String
- type RegexError = String
- regexMatch :: Regexp -> String -> Bool
- regexMatchText :: Regexp -> Text -> Bool
- regexMatchTextGroups :: Regexp -> Text -> [Text]
- regexReplace :: Regexp -> Replacement -> String -> Either RegexError String
- regexReplaceUnmemo :: Regexp -> Replacement -> String -> Either RegexError String
- regexReplaceAllBy :: Regexp -> (String -> String) -> String -> String

# Regexp type and constructors

data Regexp Source #

Regular expression. Extended regular expression-ish syntax ? But does not support eg (?i) syntax.

#### Instances

Instances details

ToJSON Regexp Source # |
Instance details Defined in Hledger.Utils.Regex Methods toJSON :: Regexp -> Value Source # toEncoding :: Regexp -> Encoding Source # toJSONList :: [Regexp] -> Value Source # toEncodingList :: [Regexp] -> Encoding Source # omitField :: Regexp -> Bool Source #
NFData Regexp Source # |
Instance details Defined in Hledger.Utils.Regex Methods rnf :: Regexp -> () Source #
Read Regexp Source # |
Instance details Defined in Hledger.Utils.Regex Methods readsPrec :: Int -> ReadS Regexp Source # readList :: ReadS [Regexp] Source # readPrec :: ReadPrec Regexp Source # readListPrec :: ReadPrec [Regexp] Source #
Show Regexp Source # |
Instance details Defined in Hledger.Utils.Regex Methods showsPrec :: Int -> Regexp -> ShowS Source # show :: Regexp -> String Source # showList :: [Regexp] -> ShowS Source #
Eq Regexp Source # |
Instance details Defined in Hledger.Utils.Regex Methods (==) :: Regexp -> Regexp -> Bool Source # (/=) :: Regexp -> Regexp -> Bool Source #
Ord Regexp Source # |
Instance details Defined in Hledger.Utils.Regex Methods compare :: Regexp -> Regexp -> Ordering Source # (<) :: Regexp -> Regexp -> Bool Source # (<=) :: Regexp -> Regexp -> Bool Source # (>) :: Regexp -> Regexp -> Bool Source # (>=) :: Regexp -> Regexp -> Bool Source # max :: Regexp -> Regexp -> Regexp Source # min :: Regexp -> Regexp -> Regexp Source #
RegexLike Regexp String Source # |
Instance details Defined in Hledger.Utils.Regex Methods matchOnce :: Regexp -> String -> Maybe MatchArray matchAll :: Regexp -> String -> [MatchArray] matchCount :: Regexp -> String -> Int matchTest :: Regexp -> String -> Bool matchAllText :: Regexp -> String -> [MatchText String] matchOnceText :: Regexp -> String -> Maybe (String, MatchText String, String)
RegexContext Regexp String String Source # |
Instance details Defined in Hledger.Utils.Regex Methods match :: Regexp -> String -> String matchM :: MonadFail m => Regexp -> String -> m String

toRegex :: Text -> Either RegexError Regexp Source #

toRegexCI :: Text -> Either RegexError Regexp Source #

toRegex' :: Text -> Regexp Source #

toRegexCI' :: Text -> Regexp Source #

# type aliases

type Replacement = String Source #

A replacement pattern. May include numeric backreferences (N).

type RegexError = String Source #

An error message arising during a regular expression operation. Eg: trying to compile a malformed regular expression, or trying to apply a malformed replacement pattern.

# total regex operations

regexMatch :: Regexp -> String -> Bool Source #

Test whether a Regexp matches a String. This is an alias for `matchTest` for consistent naming.

regexMatchText :: Regexp -> Text -> Bool Source #

Tests whether a Regexp matches a Text.

This currently unpacks the Text to a String, to work around a performance bug in regex-tdfa (#9), which may or may not be relevant here.

regexMatchTextGroups :: Regexp -> Text -> [Text] Source #

Return a (possibly empty) list of match groups derived by applying the Regex to a Text.

regexReplace :: Regexp -> Replacement -> String -> Either RegexError String Source #

A memoising version of regexReplace. Caches the result for each search pattern, replacement pattern, target string tuple. This won't generate a regular expression parsing error since that is pre-compiled nowadays, but there can still be a runtime error from the replacement pattern, eg with a backreference referring to a nonexistent match group.

regexReplaceUnmemo :: Regexp -> Replacement -> String -> Either RegexError String Source #

regexReplaceAllBy :: Regexp -> (String -> String) -> String -> String Source #