From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.emacs.gnus.general/70372 Path: news.gmane.org!not-for-mail From: Ted Zlatanov Newsgroups: gmane.emacs.devel,gmane.emacs.gnus.general Subject: Re: replace matches in any string Date: Thu, 02 Sep 2010 12:08:08 -0500 Organization: =?utf-8?B?0KLQtdC+0LTQvtGAINCX0LvQsNGC0LDQvdC+0LI=?= @ Cienfuegos Message-ID: <87r5hc2hvr.fsf@lifelogs.com> References: <87ab3gfb41.fsf@hati.baby-gnu.org> <87r5wrgg8w.fsf@lifelogs.com> <87y6gvwryp.fsf@hati.baby-gnu.org> <87d3y7p1ie.fsf@gnu.org> <87633y7v02.fsf@hati.baby-gnu.org> <87vdbyccgg.fsf@hati.baby-gnu.org> <87eiikdfub.fsf@hati.baby-gnu.org> <87ocf18ntz.fsf@hati.baby-gnu.org> <87aaq2uldu.fsf@lifelogs.com> <87y6dg5vv2.fsf@hati.baby-gnu.org> <878w4skgqn.fsf_-_@lifelogs.com> <87sk1tk1px.fsf@lifelogs.com> <87fwxs5m0h.fsf@lifelogs.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1283447373 14207 80.91.229.12 (2 Sep 2010 17:09:33 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 2 Sep 2010 17:09:33 +0000 (UTC) Cc: ding@gnus.org To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Sep 02 19:09:32 2010 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1OrDI1-0007tl-Gt for ged-emacs-devel@m.gmane.org; Thu, 02 Sep 2010 19:09:29 +0200 Original-Received: from localhost ([127.0.0.1]:47285 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OrDI0-00026Z-TO for ged-emacs-devel@m.gmane.org; Thu, 02 Sep 2010 13:09:28 -0400 Original-Received: from [140.186.70.92] (port=51717 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OrDH3-0001O0-Od for emacs-devel@gnu.org; Thu, 02 Sep 2010 13:08:35 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OrDGy-0002m2-I0 for emacs-devel@gnu.org; Thu, 02 Sep 2010 13:08:29 -0400 Original-Received: from lo.gmane.org ([80.91.229.12]:51162) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OrDGy-0002lq-67 for emacs-devel@gnu.org; Thu, 02 Sep 2010 13:08:24 -0400 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1OrDGu-000757-Bq for emacs-devel@gnu.org; Thu, 02 Sep 2010 19:08:20 +0200 Original-Received: from 38.98.147.130 ([38.98.147.130]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 02 Sep 2010 19:08:20 +0200 Original-Received: from tzz by 38.98.147.130 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 02 Sep 2010 19:08:20 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 46 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 38.98.147.130 X-Face: bd.DQ~'29fIs`T_%O%C\g%6jW)yi[zuz6; d4V0`@y-~$#3P_Ng{@m+e4o<4P'#(_GJQ%TT= D}[Ep*b!\e,fBZ'j_+#"Ps?s2!4H2-Y"sx" User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux) Cancel-Lock: sha1:T2bWspZOgf21PzaseLmDzrg1Kb4= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:129607 gmane.emacs.gnus.general:70372 Archived-At: On Thu, 02 Sep 2010 18:21:22 +0200 Stefan Monnier wrote: SM> (let ((regex "\\(alpha\\)") SM> (string "gamma alpha beta")) SM> (when (string-match (concat "\\`.*?\\(?:" regex "\\).*\\'") string) SM> (replace-match "found greek letter \\1" nil nil string))) SM> will do what you want, assuming your strings don't contain newlines, and SM> assuming that providing `string' is not a problem. SM> We could implement this feature efficiently by generalizing the `subexp' SM> argument so that rather than subgroup-number it can also take a special SM> new value `whole-string' which means "not just the whole matched text, SM> but the whole freakin' string". Then you could do: SM> (let ((regex "\\(alpha\\)") SM> (string "gamma alpha beta")) SM> (when (string-match regex string) SM> (replace-match "found greek letter \\1" nil nil string 'whole-string))) That would be great. Then we wouldn't have to play the string-match regex escaping game above. But it complicates the code a bit to provide `string'. I think Lars's suggestion to always save `string' to the same global variable is sensible; then we could say (replace-match "found greek letter \\1" nil nil 'whole-last-string) to mean "replace in the whole freakin' string you last matched on" :) In a few tests your example worked great. Lars, WDYT? In general, it would be really nice if there was a match-string/string-match/replace-match API variation that worked like Perl's $1...$9 and Perl's named captures. Those simply contain the matched substring. Putting a name on the capture is especially nice in recent Perls, so you can refer to the captured data symbolically without the fragile index. But that's not necessary for the feature I'm requesting so put it on the wishlist... SM> Of course, such string matching is a bit less optimized (and even less SM> so if you replace "." with "\\(?:.\\|\n\\)" to handle newlines), so it SM> might not be ideal. I think it will be used in non-performance-critical pieces so it's OK. Ted