lilypond-user
[Top][All Lists]
Advanced

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

Re: Multiple replacements in one regular expression?


From: Urs Liska
Subject: Re: Multiple replacements in one regular expression?
Date: Wed, 21 Jan 2015 15:13:48 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.4.0


Am 21.01.2015 um 14:52 schrieb Urs Liska:

Am 21.01.2015 um 14:45 schrieb Sven Axelsson:
On 21 January 2015 at 14:01, Urs Liska <address@hidden> wrote:>
Hm, it's not as easy as that because the escaping can be quite different,
e.g.

"&" -> "\&"
"\" -> "\textbackslash"
"[" -> "{[}"

etc.

So what I really need is:
match (&)(\)([)
and replace that with
(\&)(\textbackslash )({})

And I would be glad if I wouldn't have to do that with individual runs of
regexp-substitute.
Looks like you can use a callback function with regexp-substitute and
then do whatever
necessary there, eg. (from the Guile manual).

(regexp-substitute/global #f "[a-z]+"  "to do and not-do"
   'pre (lambda (m) (string-reverse (match:substring m))) 'post)
⇒ "ot od dna ton-od"


That looks promising, and I think I can combine that with Johan's suggestion of an association list.

Thanks to both.
Urs

It works :-)

\version "2.19.16"
#(use-modules (ice-9 regex))

str = "This should be \partcombine[or]{Apart} & match everything"

#(define escape-pairs
   '(("\\" . "\\textbackslash ")
     ("&" . "\\&")
     ("{" . "\\{")
     ("}" . "\\}")
     ("[" . "{[}")
     ("]" . "{]}")))

escape-regexp = "&|\\\\|\{|\}|\[|\]"

#(set! str
       (regexp-substitute/global #f escape-regexp str
         'pre (lambda (m)
                (assoc-ref escape-pairs (match:substring m)))
         'post))


#(ly:message "")
#(ly:message str)
#(ly:message "")


_______________________________________________
lilypond-user mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/lilypond-user




reply via email to

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