|
From: | Urs Liska |
Subject: | Multiple replacements in one regular expression? |
Date: | Wed, 21 Jan 2015 11:23:40 +0100 |
User-agent: | Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 |
Hi again, as a follow-up to my previous regex question I have another one: Can I use regular expressions to apply multiple substitutions in one go? The following code works and gives the desired result: \version "2.19.16"
#(use-modules (ice-9 regex))
str = "This should be \partcombine & match everything"
#(set! str
(regexp-substitute/global #f "\\\\" str
'pre "\\textbackslash " 'post))
#(set! str
(regexp-substitute/global #f "&" str
'pre "\\&" 'post))
=> "This should be \textbackslash partcombine \& match everything"
However, this approach will get quite cumbersome (and expensive)
when there are numerous characters to be escaped. Therefore I'd
like to know if I can "fold" them in one regular _expression_
substitution. I know that I can capture different groups to match the different
characters to be escaped, and I know how regexp-substitue allows
me to re-insert the matches in the resulting string. But that's
not what I want. One thing to note is that the order of operations matters. If I
reverse the order of the example then first the ampersand would be
prepended with the backslash and then this backslash would be
escaped with its own replacement string. TIA Urs |
[Prev in Thread] | Current Thread | [Next in Thread] |