| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Text.Regex.Lens
Synopsis
- data MatchPart text = MatchPart {
- _matchedString :: text
- _captures :: [text]
- matchedString :: forall text. Lens' (MatchPart text) text
- captures :: forall text. Getter (MatchPart text) [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' :: Traversal' (RegexResult text) (MatchPart text)
Documentation
Constructors
| MatchPart | |
Fields
| |
matchedString :: forall text. Lens' (MatchPart text) 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|] . matchedStringJust "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|] . matchedStringoverl (++ "peta").overl (++ "nya")/=overl ((++ "peta") . (++ "nya"))overl (++ "put").overl (++ "hot")==overl ((++ "put") . (++ "hot"))
matched :: (Indexable Int p, Applicative f) => p (MatchPart text) (f (MatchPart text)) -> RegexResult text -> f (RegexResult text) Source #
matched' :: Traversal' (RegexResult text) (MatchPart text) Source #