Is there a high-level API for doing search-and-replace with regexes in Haskell? In particular, I\'m looking at the Text.Regex.TDFA
or Text.Regex.Posix
maybe this approach fit you.
import Data.Array (elems)
import Text.Regex.TDFA ((=~), MatchArray)
replaceAll :: String -> String -> String -> String
replaceAll regex new_str str =
let parts = concat $ map elems $ (str =~ regex :: [MatchArray])
in foldl (replace' new_str) str (reverse parts)
where
replace' :: [a] -> [a] -> (Int, Int) -> [a]
replace' new list (shift, l) =
let (pre, post) = splitAt shift list
in pre ++ new ++ (drop l post)