lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Multiple replacements in one regular expression?


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.
What I *do* want is:
- capture a number of expressions in capture groups
- replace each capture group with a matching replacement string.

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.

Somehow I don't seem to be able to find the solution myself :-(

TIA
Urs


reply via email to

[Prev in Thread] Current Thread [Next in Thread]