Has anybody replicated =~ s/../../ or even something more basic for doing replacements with pcre haskell regexen?

Is there something like subRegex... something like =~ s/.../.../ in perl... for haskell pcre Regexen? I mean, subRegex from Text.Regex of course: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/regex-compat Thanks for any advice, thomas.

Thomas Hartman wrote:
Is there something like subRegex... something like =~ s/.../.../ in perl... for haskell pcre Regexen?
I mean, subRegex from Text.Regex of course: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/regex-compat
Thanks for any advice,
thomas.
Short answer: No. This is a FAQ. The usual answer to your follow up "Why not?" is that the design space is rather huge. Rather than justify this statement, I will point at the complicated module: http://hackage.haskell.org/packages/archive/split/0.1.1/doc/html/Data-List-S... The above module is "a wide range of strategies for splitting lists", which is a much simpler problem than your subRegex request, and only works on lists. A subRegex library should also work on bytestrings (and Seq). At the cost of writing your own routine you get exactly what you want in a screen or less of code, see http://hackage.haskell.org/packages/archive/regex-compat/0.92/doc/html/src/T... for "subRegex" which is 30 lines of code. Cheers, Chris

On Fri, Mar 13, 2009 at 1:19 AM, ChrisK
.... At the cost of writing your own routine you get exactly what you want in a screen or less of code, see http://hackage.haskell.org/packages/archive/regex-compat/0.92/doc/html/src/T... for "subRegex" which is 30 lines of code.
WTF! Cristiano

tphyahoo:
Is there something like subRegex... something like =~ s/.../.../ in perl... for haskell pcre Regexen?
I mean, subRegex from Text.Regex of course: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/regex-compat
Thanks for any advice,
Basically, we should have it.

Don Stewart wrote:
tphyahoo:
Is there something like subRegex... something like =~ s/.../.../ in perl... for haskell pcre Regexen?
I mean, subRegex from Text.Regex of course: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/regex-compat
Thanks for any advice,
Basically, we should have it.
Let me open the discussion with all the questions I can quickly ask: What should the "subRegex" function do, exactly? (Single replacement,global replacement,once per line,...) What should the replacement template be able to specify? (Can it refer to all text before a match or all text after?) (Can it access the start/stop offsets as numbers?) Should the replacement template be specified in a "String"? As an abstract data type or syntax tree? With combinators? What happens if the referenced capture was not made? Empty text? How will syntax errors in the template be handled (e.g. referring to a capture that does not exist in the regular expression)? Will the output text be String? ByteString? ByteString.Lazy? Seq Char? Note: String and Strict Bytestrings are poor with concatenation. Can the output text type differ from the input text type? -- Chris

2009/3/16 ChrisK
Let me open the discussion with all the questions I can quickly ask:
What should the "subRegex" function do, exactly? (Single replacement,global replacement,once per line,...)
Try to do the same thing as =~ s/../../ in perl. For a version 1: Global replacements, don't treat newlines separately, ^ and $ anchor at start and end of string. There could be a Bool option to support multiline replacement modes.
What should the replacement template be able to specify? (Can it refer to all text before a match or all text after?) (Can it access the start/stop offsets as numbers?)
Again, follow =~ s/../../ I'm not sure what =~ allows in this dimension though. My instinct is
(Can it refer to all text before a match or all text after?)
no
(Can it access the start/stop offsets as numbers?)
no But maybe that's just because I've never needed the above functionality. I basically think of =~ s as "quick cleanup for dirty text" solution, nothing approaching full-fledged parsing.
Should the replacement template be specif~ied in a "String"?
Sure, just like it is in Text.Regex.subRegex now. No combinators, \numbered capture references are fine.
As an abstract data type or syntax tree? With combinators?
Just a string I think.
What happens if the referenced capture was not made? Empty text?
Return the original string. Isn't that what subRegex already does?
How will syntax errors in the template be handled (e.g. referring to a capture that does not exist in the regular expression)?
runtime error
Will the output text be String? ByteString? ByteString.Lazy? Seq Char? Note: String and Strict Bytestrings are poor with concatenation.
String. Add support for others if users holler for it
Can the output text type differ from the input text type?
Nah. My 2c.
-- Chris
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
ChrisK
-
Cristiano Paris
-
Don Stewart
-
Thomas Hartman