From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Thierry Volpiatto Newsgroups: gmane.emacs.devel Subject: Re: Improve `replace-regexp-in-string' ergonomics? Date: Wed, 22 Sep 2021 07:47:14 +0000 Message-ID: <87wnn9gigt.fsf@posteo.net> References: <878rzpw7jo.fsf@gnus.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7769"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Lars Ingebrigtsen , Emacs developers To: Yuri Khan Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Sep 22 09:50:22 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mSx1K-0001p0-2G for ged-emacs-devel@m.gmane-mx.org; Wed, 22 Sep 2021 09:50:22 +0200 Original-Received: from localhost ([::1]:58998 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mSx1G-0003lz-1j for ged-emacs-devel@m.gmane-mx.org; Wed, 22 Sep 2021 03:50:18 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42216) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mSwyb-00019u-Hs for emacs-devel@gnu.org; Wed, 22 Sep 2021 03:47:33 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:53953) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mSwyV-0004CA-Sl for emacs-devel@gnu.org; Wed, 22 Sep 2021 03:47:33 -0400 Original-Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id 0BB5C240037 for ; Wed, 22 Sep 2021 09:47:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1632296844; bh=WpYvVgWFALUFLEhp+OAzDVJpmUKJl7hgLS8+Cpvmr8g=; h=From:To:Cc:Subject:Autocrypt:Date:From; b=cF9bjEAm9bplubCcl63qJUtUzpDwAB0s77mnl8ad99J11dKJqytfhBn9TymQkC9My TIRGrGQPWZSgx2YLPg+nPO1GWRKTYQXb8MHJaXEoqu6qnz9iZSNr8h/mFaSQMkq0+I u5V3HJ4QyivbfymdleWgK/3cv6Iiq8Z/96uesFdseHSAZ4rp7vew/GUTELiIwyZkNY wyNmdiofYt6gdvb3lfNT0Z6Z6ftpiHePZY+YsDXqYVpm3Us/vYj5W6Dzfhn8ETd10u /ACOUnukQZuGz2P72cDMAOCWbodGb+a8vbYYoh8RCNHWKKLJg3g7EnvIpLOBHM0owi OqKQvmXZxXGCw== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4HDr3s5Xvpz9rxw; Wed, 22 Sep 2021 09:47:21 +0200 (CEST) Autocrypt: addr=thievol@posteo.net; prefer-encrypt=mutual; keydata= mQGNBF8ylcIBDADG+hy+zR6L4/vbdDDZuSaMmSrU3A5QZJpeBCvxTr7MpzzruZbhLPW1K3R6N2MA edi8Y+C8o27FVRIjpdbaKMGu9je7JV/TbUQYo3SOwCK1vM4LUn4V6ZLzSYkuiEt4eyMoiDdyvN0p kcK6P9x9DCetcEVszXzQg+yzCVrQ2hXWDXWT4M18EC3wtO7RHPouMqGiwBFhBAYErCqFWFxQHkfb tG/4yGyJ58rglb65O3qijjMWvYwcWZun9/7qm8Z4/4mHopmo2zgU+OrptnLSZfkZGz3Y7Uf452xQ GVq0Fv75NPvQru7y+DYVhuVXXyAmGxt+vf4rIiixMBbhKEPjcxEPAa2LTzex2IsTZR+QVG9uDnqC WcgaOEQ58fzXNvNhtwwF/Rgio2XWAJVdmFWS59/k9W58CIUSNKBMZh2XeGdEmtHvDtCxW3z6FJha 36RzOM3fMNNiAGdFZJA84gcdloJR+sHCDTTPT3784fjr+V8An7sI581NGFzkRQqPvEQCZbUAEQEA AbQSdGhpZXZvbEBwb3N0ZW8ubmV0iQHUBBMBCgA+FiEEI9twfRN7r3nig/xwDsVtFB0W75MFAl8y lcICGwMFCQPCZwAFCwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQDsVtFB0W75MB3QwAlTsVzFmr +S/tMKwwwOibjhNPi/OZiUC2AYfaqfVAiIHDT3RbzDe03sAJoomJkJnYVjGzQZwibCMO2+ITkMPV 2wvrd4CbgS1KCVbrltwcuK/nxPCBaHytOCZUIInnhJo5PE/h03K In-Reply-To: (Yuri Khan's message of "Wed, 22 Sep 2021 12:22:05 +0700") Received-SPF: pass client-ip=185.67.36.65; envelope-from=thievol@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:275300 Archived-At: Yuri Khan writes: > However, I have a hypothesis that what users really want is =E2=80=9CI ha= ve > 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=E2=80=9D, without thinking about intermedia= te > 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.) Instead of using a list like=20 (pattern replacement pattern replacement etc...)=20 perhaps a list like (pattern subexp replacement ...) then you can loop in this list doing the replacement with those 3 elements, something like this: (defun tv/replace-regexp-in-string (regexps string &optional fixedcase = literal) (cl-assert (zerop (% (length regexps) 3))) (cl-loop with str =3D string for (m s r) on regexps by 'cdddr do (setq str (replace-regexp-in-string m r str fixedcase literal s)) finally return str)) With previous example it give: (let ((results "['foo', 'bar', 'baz']")) (tv/replace-regexp-in-string '("\\[" 0 "(" "\\]" 0 ")" ",[[:space:]]" 0 " " "'" 0 "\"") results)) "(\"foo\" \"bar\" \"baz\")" --=20 Thierry