Safe Haskell | None |
---|---|
Language | Haskell2010 |
Text.Regex.Lens
Synopsis
- data MatchPart text = MatchPart {
- _matchedString :: text
- _captures :: [text]
- matchedString :: forall text f. Functor f => (text -> f text) -> MatchPart text -> f (MatchPart text)
- captures :: forall text f. (Contravariant f, Functor f) => ([text] -> f [text]) -> MatchPart text -> f (MatchPart text)
- regex :: (RegexLike regex text, Monoid text) => regex -> IndexedTraversal' Int text (MatchPart text)
- regex' :: (RegexLike regex text, Monoid text) => regex -> Lens' text (RegexResult text)
- matched :: (Indexable Int p, Applicative f) => p (MatchPart text) (f (MatchPart text)) -> RegexResult text -> f (RegexResult text)
- matched' :: forall text f. Applicative f => (MatchPart text -> f (MatchPart text)) -> RegexResult text -> f (RegexResult text)
Documentation
Constructors
MatchPart | |
Fields
|
matchedString :: forall text f. Functor f => (text -> f text) -> MatchPart text -> f (MatchPart text) Source #
captures :: forall text f. (Contravariant f, Functor f) => ([text] -> f [text]) -> MatchPart text -> f (MatchPart text) Source #
Arguments
:: (RegexLike regex text, Monoid text) | |
=> regex | compiled regular expression |
-> IndexedTraversal' Int text (MatchPart text) |
An indexed Traversal for matched part with regexp.
>>>
"foo bar baz" ^? regex [r|b.*r|]
Just (MatchPart {_matchedString = "bar", _captures = []})
>>>
"foo bar baz" ^? regex [r|hoge|]
Nothing
You can access to the matched string by using matchedString
:
>>>
"foo bar baz" ^? regex [r|b.*r|] . matchedString
Just "bar"
Multiple result:
>>>
"foo bar baz" ^.. regex [r|b[^ ]+|] . matchedString
["bar","baz"]
Replace:
>>>
"foo bar baz" & regex [r|b[^ ]+|] . matchedString .~ "nya"
"foo nya nya"
Indexing:
>>>
"foo bar baz" ^.. regex [r|b[^ ]+|] . index 1 . matchedString
["baz"]
>>>
"foo bar baz" & regex [r|b[^ ]+|] . index 1 . matchedString .~ "nya"
"foo bar nya"
Captures:
>>>
"foo00 bar01 baz02" ^.. regex [r|([a-z]+)([0-9]+)|] . captures
[["foo","00"],["bar","01"],["baz","02"]]
>>>
"foo00 bar01 baz02" ^.. regex [r|([a-z]+)([0-9]+)|] . captures . traversed . index 1
["00","01","02"]
Note: This is not a legal Traversal, unless you are very careful not to invalidate the predicate on the target.
For example, if you replace the matched part with a string which is not match with the regex, the second Traversal
law is violated.
let l = regex [r|t.*t|] . matchedStringover
l (++ "peta").
over
l (++ "nya")/=
over
l ((++ "peta") . (++ "nya"))over
l (++ "put").
over
l (++ "hot")==
over
l ((++ "put") . (++ "hot"))