Reputation: 983
I'm trying to use the regex package (with TDFA) and the only reference I can find for named captures group is a quasi quoter (cp) and no explanation. Basically I have regexs in a config files, compiled at runtime with compileRegex, applied to lines and I'm looking to extract a couple of specific captures group out of that, with names I do know at compile time.
So I'm looking for a function that would take the Matches a
, a string (the capture group name) and would return I guess a Maybe a
, depending on whether that capture group did match or not ? Or even just a way to extract the capture group names from the Match a
, that would be enough.
The doc mentions the quasi quoter cp, but no explanation as to how it's supposed to be used, and I'm not even sure I could use if if I knew because I compile my regex at runtime.
Would anyone have examples with named capture groups ? Thanks
Upvotes: 1
Views: 159
Reputation: 1526
You'll find the API you need in the Text.RE.Replace docs; I think one of the functions with names that start with capture
will be what you're looking for. Here's an example:
module Main where
import Data.Text
import Text.RE.Replace
import Text.RE.TDFA.String
needle :: CaptureID
needle = IsCaptureName . CaptureName . pack $ "needle"
main :: IO ()
main = do
re <- compileRegex "-${needle}([a-z]+)-"
let match = "haystack contains the -word- you want" ?=~ re
if matched match
then print $ captureText needle match
else print "(no match)"
Upvotes: 1