[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Improve `replace-regexp-in-string' ergonomics?
From: |
Stefan Monnier |
Subject: |
Re: Improve `replace-regexp-in-string' ergonomics? |
Date: |
Wed, 22 Sep 2021 14:14:17 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
> (replace-regexp-in-string
> "\\[" "(" (replace-regexp-in-string
> "\\]" ")" (replace-regexp-in-string
> ",[[:space:]]" " "
> (replace-regexp-in-string
> "'" "\"" results))))))
To me "the right way" is to pass a function as the replacement.
I'm not sure the way REP currently works in `replace-regexp-in-string`
is the best we can do, but it works:
(replace-regexp-in-string
"[][']\\|,[[:space:]]\\(\\)"
(lambda (s)
(if (match-end 1) " "
(pcase-exhaustive (aref s 0)
(?\[ "(")
(?\] ")")
(?\' "\""))))
results)
It'd be nice to have a front-end that lets you write a kind of lex-like
set of rules, but I think it'd be important for that front-end to allow
*computing* the replacement rather than only selecting it based on the
matched regexp. Something like
(foo-replace results
("\\[" "(")
("\\]" ")")
("[[:lower:]]+" (upcase (match-string 0)))
("siglo \\([0-9]+\\)"
(integer-to-roman (string-to-number (match-string 1))))
...)
-- Stefan
- Re: Improve `replace-regexp-in-string' ergonomics?, (continued)
Re: Improve `replace-regexp-in-string' ergonomics?, Lars Ingebrigtsen, 2021/09/22
Re: Improve `replace-regexp-in-string' ergonomics?, Dmitry Gutov, 2021/09/22
Re: Improve `replace-regexp-in-string' ergonomics?,
Stefan Monnier <=