all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuri Khan <yuri.v.khan@gmail.com>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: Emacs developers <emacs-devel@gnu.org>
Subject: Re: Improve `replace-regexp-in-string' ergonomics?
Date: Wed, 22 Sep 2021 12:22:05 +0700	[thread overview]
Message-ID: <CAP_d_8X64-MMx3OtbOCWWkkrRnb27RobY6Qe9O0CP9N-ZVYvKw@mail.gmail.com> (raw)
In-Reply-To: <878rzpw7jo.fsf@gnus.org>

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	other threads:[~2021-09-22  5:22 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-22  4:36 Improve `replace-regexp-in-string' ergonomics? Lars Ingebrigtsen
2021-09-22  5:22 ` Yuri Khan [this message]
2021-09-22  6:36   ` Lars Ingebrigtsen
2021-09-22  7:47   ` Thierry Volpiatto
2021-09-22  5:24 ` Po Lu
2021-09-22  6:37   ` Lars Ingebrigtsen
2021-09-22 10:56     ` Po Lu
2021-09-22 20:08       ` Lars Ingebrigtsen
2021-09-23  0:11         ` Po Lu
2021-09-22  7:33 ` Adam Porter
2021-09-22  8:09   ` Lars Ingebrigtsen
2021-09-22  7:51 ` Andreas Schwab
2021-09-22  8:14 ` Augusto Stoffel
2021-09-22  8:21   ` Adam Porter
2021-09-22 18:01     ` Stefan Monnier
2021-09-22 18:24       ` Basil L. Contovounesios
2021-09-22 22:56       ` Adam Porter
2021-09-22 23:53         ` Eric Abrahamsen
2021-09-22 20:06   ` Lars Ingebrigtsen
2021-09-22 10:59 ` Dmitry Gutov
2021-09-22 20:18   ` Lars Ingebrigtsen
2021-09-22 22:23     ` Dmitry Gutov
2021-09-22 23:24       ` [External] : " Drew Adams
2021-09-22 18:14 ` Stefan Monnier
2021-09-22 19:30   ` Mattias Engdegård
2021-09-22 20:22   ` Lars Ingebrigtsen
2021-09-22 20:29     ` Lars Ingebrigtsen
2021-09-23  2:15     ` Stefan Monnier
2021-10-05 16:18 ` Juri Linkov
2021-10-12  6:53   ` Juri Linkov
2021-10-12 12:10     ` Lars Ingebrigtsen
2021-10-12 12:34       ` Stefan Monnier
2021-10-12 12:41         ` Lars Ingebrigtsen
2021-10-12 13:18           ` Lars Ingebrigtsen
2021-10-12 13:32             ` Mattias Engdegård
2021-10-12 15:48             ` Stefan Monnier
2021-10-12 13:33           ` Thierry Volpiatto
2021-10-12 19:16             ` Juri Linkov
2021-10-12 20:44               ` Thierry Volpiatto
2021-10-13  7:57                 ` Juri Linkov
2021-10-13  8:41                   ` Thierry Volpiatto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAP_d_8X64-MMx3OtbOCWWkkrRnb27RobY6Qe9O0CP9N-ZVYvKw@mail.gmail.com \
    --to=yuri.v.khan@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=larsi@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.