Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4049922
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:00:21+00:00 2026-05-20T14:00:21+00:00

The problem is quite simple: I have to replace all occurences of fooo and

  • 0

The problem is quite simple: I have to replace all occurences of “fooo” and all its substrings with “xyz”. In Java, for example, I will do it like this:

someString.replaceAll( "fooo|foo|fo", "xyz" )

and it will do the trick. But in Haskell I’ve found no efficient way to work with regex. First of all, I’ve read this: http://www.haskell.org/haskellwiki/Regular_expressions

The only library which actually has replace function is regex-posix, but its considered “very slow” in performance. And this fact is not acceptable. Also I’ve found that this replace function for any reasons doesn’t respect the order of patterns given, so I’ve got output like this:

>replace "boo fooo boo" "xyz"
"boo xyzoo boo"

Other backends don’t imply such functionality.

So I decided to write simple workaround:

replaceFoo input =
    helper input []
    where
        helper ('f':'o':'o':'o':xs) ys = helper xs ("zyx" ++ ys)
        helper ('f':'o':'o':xs) ys = helper xs ("zyx" ++ ys)
        helper ('f':'o':xs) ys = helper xs ("zyx" ++ ys)
        helper (x:xs) ys = helper xs (x:ys)
        helper [] ys = reverse ys

Whilst I don’t find this function nice, it works well and fast. But for now I met the necessity to add more words in this replacor, and I don’t like the idea to extend helper patterns anymore (I need to say that I actually have 4 words in it in real app and that’s odd).

I’ll be happy if someone help me with fast solution.


cebewee, thanks for the Data.String.Utils. But I fear this approach is quite slow if there are many words to replace (“fooo” to “xyz”, “foo” to “xyz”, “fo” to “xyz”, “bar” to “quux” and so on), because to get that to work I will need to foldr (\str (from,to) -> replace from to str) input pairs or something like that and it will take O(n*n). More than that, it may have unexpected result of replacing substring of result of previous replacement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:00:22+00:00Added an answer on May 20, 2026 at 2:00 pm

    The regex-xmlschema package has a sed function that might be what you’re looking for:

    http://hackage.haskell.org/package/regex-xmlschema-0.1.3

    See particularly:

    http://hackage.haskell.org/packages/archive/regex-xmlschema/0.1.3/doc/html/Text-Regex-XMLSchema-String.html#v:sed

    There was a discussion of options for string rewriting on Haskell-Cafe last year:

    http://www.haskell.org/pipermail/haskell-cafe/2010-May/077943.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is quite simple: I have a 400MB file filled with 10,000,000 lines
I have a problem that is quite simple to understand, but for me it
I have a seemingly simple problem, but I can't quite figure out a solution.
The problem is quite simple, I have a method that returns a list. I
I have a probably quite simple to solve problem. I'm trying to parse this
I have a (hopefully quite simple) Javascript problem. I've search but found nothing that
I do have troubles manipulating hashmaps. My problem is quite simple, but I cannot
My problem is quite simple Suppose I have those class: public class A {
I have this problem (I think it's quite simple, however, I cannot solve it):
I'm using UIImagePickerController to take pictures. It's quite simple. But I have a problem:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.