All Unkept
Posted in: Haskell  —  30 July 2008

Haskell Regex replace

Does anyone have a function for doing a regex replace in Haskell? I found this: regex replace example but it is about 2 years old and doesn't compile on my machine (even after adding {-# OPTIONS_GHC -fglasgow-exts -fbang-patterns #-}), and I'm too lazy much of a newbie to work out how to fix it.

Also, I need a solution that uses the regex-pcre or pcre-light libraries, rather than regex-posix. Any help would be much appreciated.

Comments §

§ On 30 July 2008, Luke Palmer wrote:
331 Hmm, that's odd. The first example compiles for me (GHC 6.8.2, -XBangPatterns). You might try asking haskell-cafe or something.

§ On 31 July 2008, ptolomy wrote:
332 Wow, that is my old code.
Both compile for me on 6.8.2 using "-fbang-patterns --make".

It's my understanding that regex-pcre should be a drop-in replacement for Text.Regex; I haven't used it, but based on the instances it defines according to hackage, using it with that old code should be pretty simple.

§ On 31 July 2008, ptolomy wrote:
333 (I've posted a much better version to hpaste)

§ On 31 July 2008, luke wrote:
334 Hey, thanks for the response, I've got the same problem. Details here: http://hpaste.org/9295

§ On 2 August 2008, Spai wrote:
336 Wouldnt something like this work for you?

import Text.Regex(subRegex,mkRegex)

-- substitute x with y in s
substitute x y s = subRegex (mkRegex x) s y


§ On 4 August 2008, luke wrote:
338 Spai: thanks, I'll try that out.

I've found the cause of my bug (in vague terms): bytestring-0.9.1.0 causes the compilation failure. Rverting to 0.9.0.1 makes it work again.

§ On 5 August 2008, Chris K wrote:
343 I publish regex-pcre and regex-posix, etc. What is the compilation failure?

Also: The number of imaginable replacement routines is quite large, and no general solution can also be efficient. Rolling you own is usually the right choice.

Add comment

Format:

  • Javascript has to be on to get past my spam protection, and cookies, and there is a delay, sorry for any inconvenience!
  • I reserve the right to moderate comments.