Builds a traversal over text using a Regex pattern
It's a
QuasiQuoter which creates a Traversal out of the given
regex string. It's equivalent to calling
regexing on a
Regex created using the
re QuasiQuoter.
The "real" type is:
regex :: Regex -> IndexedTraversal' Int BS.ByteString Match
It's a traversal which selects
Matches; compose it with
match or
groups to get the relevant parts of your match.
>>> txt = "raindrops on roses and whiskers on kittens"
Search
>>> has ([regex|whisk|]) txt
True
Get matches
>>> txt ^.. [regex|\br\w+|] . match
["raindrops","roses"]
Edit matches
>>> txt & [regex|\br\w+|] . match %~ Char8.intersperse '-' . Char8.map toUpper
"R-A-I-N-D-R-O-P-S on R-O-S-E-S and whiskers on kittens"
Get Groups
>>> txt ^.. [regex|(\w+) on (\w+)|] . groups
[["raindrops","roses"],["whiskers","kittens"]]
Edit Groups
>>> txt & [regex|(\w+) on (\w+)|] . groups %~ reverse
"roses on raindrops and kittens on whiskers"
Get the third match
>>> txt ^? [regex|\w+|] . index 2 . match
Just "roses"
Edit matches
>>> txt & [regex|\br\w+|] . match %~ Char8.intersperse '-' . Char8.map toUpper
"R-A-I-N-D-R-O-P-S on R-O-S-E-S and whiskers on kittens"
Get Groups
>>> txt ^.. [regex|(\w+) on (\w+)|] . groups
[["raindrops","roses"],["whiskers","kittens"]]
Edit Groups
>>> txt & [regex|(\w+) on (\w+)|] . groups %~ reverse
"roses on raindrops and kittens on whiskers"
Get the third match
>>> txt ^? [regex|\w+|] . index 2 . match
Just "roses"
Match integers,
Read them into ints, then sort them in-place
dumping them back into the source text afterwards.
>>> "Monday: 29, Tuesday: 99, Wednesday: 3" & partsOf ([regex|\d+|] . match . from packedChars . _Show @Int) %~ sort
"Monday: 3, Tuesday: 29, Wednesday: 99"
To alter behaviour of the regex you may wish to pass
PCREOptions when compiling it. The default behaviour may seem
strange in certain cases; e.g. it operates in 'single-line' mode. You
can
compile the
Regex separately and add any options you
like, then pass the resulting
Regex into
regex;
Alternatively can make your own version of the QuasiQuoter with any
options you want embedded by using
mkRegexQQ.