From 549c47bca8a9c925872e40e2c4e85e5f41f00dc4 Mon Sep 17 00:00:00 2001 From: Jonathan Dowland Date: Thu, 31 Aug 2023 18:28:04 +0100 Subject: [PATCH] ;dev: add Utils.Regex.regexMatchTextGroups A matcher function which returns the list of match-groups, which may be empty. Signed-off-by: Jonathan Dowland --- hledger-lib/Hledger/Utils/Regex.hs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/hledger-lib/Hledger/Utils/Regex.hs b/hledger-lib/Hledger/Utils/Regex.hs index fd9658066..0b39c85a5 100644 --- a/hledger-lib/Hledger/Utils/Regex.hs +++ b/hledger-lib/Hledger/Utils/Regex.hs @@ -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