As an expression
regex :: (Alternative f) => String -> Text -> f (Captures info)
if there are parenthesized captures, or
regex :: (Alternative f) => String -> Text -> f Text
if there are none. In other words, if there is more than the 0th
capture, this behaves like
captures (except returning an opaque
Captures instead of a
NonEmpty list), otherwise it
behaves like
match.
To retrieve an individual capture from a
Captures, use
capture.
case [regex|(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})|] "submitted 2020-10-20" of
Just cs ->
let date = capture @0 cs
year = read @Int $ Text.unpack $ capture @"y" cs
...
forM_ @Maybe ([regex|\s+$|] line) $ \spaces ->
printf "line has trailing spaces (%d characters)\n" (Text.length spaces)
As a pattern
This matches when the regex first matches. Any named captures are
bound to variables of the same names.
case "submitted 2020-10-20" of
[regex|(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})|] ->
let year = read @Int $ Text.unpack y
...
Note that it is not possible to access the 0th capture this way. As a
workaround, explicitly capture the whole pattern and name it.
If there are no named captures, this simply acts as a guard.