31

Despite the ridiculously large number of regex matching engines for Haskell, the only one I can find that will substitute is Text.Regex, which, while decent, is missing a few thing I like from pcre. Are there any pcre-based packages which will do substitution, or am I stuck with this?

3
  • 3
    Good question, I've run into the same problem. Oct 2, 2010 at 22:54
  • 2
    okay, screw that, I wrote my own based on Text.Regex.PCRE.Light, although it ports nicely to most other implementations.
    – muhmuhten
    Oct 3, 2010 at 2:32
  • 6
    Post that as answer if it solves the problem.
    – kennytm
    Oct 3, 2010 at 6:40

3 Answers 3

10

I don't think "just roll your own" is a reasonable answer to people trying to get actual work done, in an area where every other modern language has a trivial way to do this. Including Scheme. So here's some actual resources; my code is from a project where I was trying to replace "qql foo bar baz qq" with text based on calling a function on the stuff inside the qq "brackets", because reasons.

Best option: pcre-heavy:

      let newBody = gsub [re|\s(qq[a-z]+)\s(.*?)\sqq\s|] (unWikiReplacer2 titles) body in do
[snip]
unWikiReplacer2 :: [String] -> String -> [String] -> String
unWikiReplacer2 titles match subList = case length subList > 0 of
        True -> " --" ++ subList!!1 ++ "-- "
        False -> match

Note that pcre-heavy directly supports function-based replacement, with any string type. So nice.

Another option: pcre-light with a small function that works but isn't exactly performant:

    let newBody = replaceAllPCRE "\\s(qq[a-z]+)\\s(.*?)\\sqq\\s" (unWikiReplacer titles) body in do
[snip]
unWikiReplacer :: [String] -> (PCRE.MatchResult String) -> String
unWikiReplacer titles mr = case length subList > 0 of
        True -> " --" ++ subList!!1 ++ "-- "
        False -> PCRE.mrMatch mr
    where
        subList = PCRE.mrSubList mr

-- A very simple, very dumb "replace all instances of this regex
-- with the results of this function" function.  Relies on the
-- MatchResult return type.
--
-- https://github.com/erantapaa/haskell-regexp-examples/blob/master/RegexExamples.hs
-- was very helpful to me in constructing this
--
-- I also used
-- https://github.com/jaspervdj/hakyll/blob/ea7d97498275a23fbda06e168904ee261f29594e/src/Hakyll/Core/Util/String.hs
replaceAllPCRE :: String              -- ^ Pattern
           -> ((PCRE.MatchResult String) -> String)  -- ^ Replacement (called on capture)
           -> String              -- ^ Source string
           -> String              -- ^ Result
replaceAllPCRE pattern f source =
    if (source PCRE.=~ pattern) == True then
      replaceAllPCRE pattern f newStr
    else
      source
    where
        mr = (source PCRE.=~ pattern)
        newStr = (PCRE.mrBefore mr) ++ (f mr) ++ (PCRE.mrAfter mr)

Someone else's fix: http://0xfe.blogspot.com/2010/09/regex-substitution-in-haskell.html

Another one, this time embedded in a major library: https://github.com/jaspervdj/hakyll/blob/master/src/Hakyll/Core/Util/String.hs

Another package for this purpose: https://hackage.haskell.org/package/pcre-utils

3

Update 2020

I totally agree with @rlpowell that

I don't think "just roll your own" is a reasonable answer to people trying to get actual work done, in an area where every other modern language has a trivial way to do this.

At the time of this writing, there is also Regex.Applicative.replace for regex substitution, though it's not Perl-compatible.

For pattern-matching and substitution with parsers instead of regex, there is Replace.Megaparsec.streamEdit

2

The regular expression API in regex-base is generic to the container of characters to match. Doing some kind of splicing generically to implements substitution would be very hard to make efficient. I did not want to provide a crappy generic routine.

Writing a small function to do the substitution exactly how you want is just a better idea, and it can be written to match your container.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.