emacs-devel
[Top][All Lists]
Advanced

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

Re: Improve `replace-regexp-in-string' ergonomics?


From: Yuri Khan
Subject: Re: Improve `replace-regexp-in-string' ergonomics?
Date: Wed, 22 Sep 2021 12:22:05 +0700

On Wed, 22 Sept 2021 at 11:37, Lars Ingebrigtsen <larsi@gnus.org> wrote:

> `replace-regexp-in-string' often leads to pretty awkward code.  I wonder
> whether we could improve it somehow.

> We could also consider making `regexp-replace' take a series of pairs,
> since this is so common.  Like:
>
>        (org-babel-read
>         (concat "'"
>                 (regexp-replace "'" "\""
>                                 ",[[:space:]]" " "
>                                 "\\]" ")"
>                                 "\\[" "("
>                                 results)))

I want to highlight a possible difference in semantics and usability
improvement with this interface, roughly parallel to (let …) vs (let*
…).

A nested/threaded/sequential imperative regexp-replace first changes
each occurrence of the first pattern into a first replacement, then
changes each occurrence of each subsequent pattern. This can lead to
interference when an earlier replacement matches a later pattern.

However, I have a hypothesis that what users really want is “I have
this list of pattern/replacement pairs, and I want to go through each
occurrence of each pattern in the original text, and replace them with
their respective replacements”, without thinking about intermediate
buffer contents. In simple cases, this can be simulated by
parenthesizing each pattern, joining them all with a \|, and using the
resulting super-pattern to iterate through occurrences, then,
depending on which group matched, doing the replacement. (Harder cases
include those where patterns have capturing groups and/or
backreferences.)



reply via email to

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