replaceRegex :: Source -> Match -> ([(Source, (MatchOffset, MatchLength))] -> Maybe Source) -> Source
replaceRegex source match trans = case source =~+ match of
      m@((_, (offset, length)):_) -> let before = LBS.take (fromIntegral offset) source
                                         after = LBS.drop (fromIntegral $ offset + length) source
                                     in case trans m of
                                          Just m' -> before <> m' <> after
                                          Nothing -> source


-- | Replace all matches with result of a function of the match.
replaceRegex :: Source -> Match -> ([(Source, (MatchOffset, MatchLength))] -> Maybe Source) -> Source
replaceRegex source match trans = case source =~+ match of
      m@((_, (offset, length)):_) -> let before = LBS.take (fromIntegral offset) source
                                         after = LBS.drop (fromIntegral $ offset + length) source
                                     in case trans m of
                                          Just m' -> before <> m' <> after
                                          Nothing -> source


-- | Replace all matches with result of a function of the match.
replaceRegex source match trans = case source =~+ match of
      m@((_, (offset, length)):_) -> let before = LBS.take (fromIntegral offset) source
                                         after = LBS.drop (fromIntegral $ offset + length) source
                                     in case trans m of
                                          Just m' -> before <> m' <> after
                                          Nothing -> source


-- | Replace all matches with result of a function of the match.