;dev: add Utils.Regex.regexMatchTextGroups

A matcher function which returns the list of match-groups, which may
be empty.

Signed-off-by: Jonathan Dowland <jon@dow.land>
This commit is contained in:
Jonathan Dowland 2023-08-31 18:28:04 +01:00 committed by Simon Michael
parent c619e387ea
commit 549c47bca8

View File

@ -55,6 +55,7 @@ module Hledger.Utils.Regex (
-- * total regex operations
,regexMatch
,regexMatchText
,regexMatchTextGroups
,regexReplace
,regexReplaceUnmemo
,regexReplaceAllBy
@ -72,7 +73,8 @@ import qualified Data.Text as T
import Text.Regex.TDFA (
Regex, CompOption(..), defaultCompOpt, defaultExecOpt,
makeRegexOptsM, AllMatches(getAllMatches), match, MatchText,
RegexLike(..), RegexMaker(..), RegexOptions(..), RegexContext(..)
RegexLike(..), RegexMaker(..), RegexOptions(..), RegexContext(..),
(=~)
)
@ -166,6 +168,14 @@ regexMatch = matchTest
regexMatchText :: Regexp -> Text -> Bool
regexMatchText r = matchTest r . T.unpack
-- | Return a (possibly empty) list of match groups derived by applying the
-- Regex to a Text.
regexMatchTextGroups :: Regexp -> Text -> [Text]
regexMatchTextGroups r txt = let
pat = reString r
(_,_,_,matches) = txt =~ pat :: (Text,Text,Text,[Text])
in matches
--------------------------------------------------------------------------------
-- new total functions