From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel,gmane.emacs.gnus.general Subject: Re: replace matches in any string Date: Thu, 02 Sep 2010 18:21:22 +0200 Message-ID: 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; charset=us-ascii X-Trace: dough.gmane.org 1283444863 2661 80.91.229.12 (2 Sep 2010 16:27:43 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 2 Sep 2010 16:27:43 +0000 (UTC) Cc: ding@gnus.org, emacs-devel@gnu.org To: Ted Zlatanov Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Sep 02 18:27:41 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 1OrCdY-0003v6-EP for ged-emacs-devel@m.gmane.org; Thu, 02 Sep 2010 18:27:40 +0200 Original-Received: from localhost ([127.0.0.1]:42590 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OrCdX-0001z6-IV for ged-emacs-devel@m.gmane.org; Thu, 02 Sep 2010 12:27:39 -0400 Original-Received: from [140.186.70.92] (port=39605 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OrCXY-00078R-Ep for emacs-devel@gnu.org; Thu, 02 Sep 2010 12:21:29 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OrCXW-0001lq-Na for emacs-devel@gnu.org; Thu, 02 Sep 2010 12:21:28 -0400 Original-Received: from impaqm1.telefonica.net ([213.4.138.1]:46940) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OrCXW-0001lI-Cx for emacs-devel@gnu.org; Thu, 02 Sep 2010 12:21:26 -0400 Original-Received: from IMPmailhost4.adm.correo ([10.20.102.125]) by IMPaqm1.telefonica.net with bizsmtp id 1rBv1f00G2iL0W201sMQLJ; Thu, 02 Sep 2010 18:21:24 +0200 Original-Received: from ceviche.home ([88.9.47.113]) by IMPmailhost4.adm.correo with BIZ IMP id 1sMP1f0052SXESY1ksMPjA; Thu, 02 Sep 2010 18:21:24 +0200 X-Brightmail-Tracker: AAAAAA== X-TE-authinfo: authemail="monnier$movistar.es" |auth_email="monnier@movistar.es" X-TE-AcuTerraCos: auth_cuTerraCos="cosuitnetc01" Original-Received: by ceviche.home (Postfix, from userid 20848) id EA491660DF; Thu, 2 Sep 2010 18:21:22 +0200 (CEST) In-Reply-To: <87fwxs5m0h.fsf@lifelogs.com> (Ted Zlatanov's message of "Thu, 02 Sep 2010 08:10:38 -0500") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) 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:129602 gmane.emacs.gnus.general:70370 Archived-At: > (let ((regex "\\(alpha\\)") > (string "gamma alpha beta")) > (when (string-match regex string) > (replace-match "[first greek letter \\1]" nil nil string 1))) > -> "gamma [first greek letter alpha] beta" > We want the equivalent but against any string, not just the original > string (the `replace-match' docs say STRING has to be the one used for > the original `string-match'). The use case is when you say to match X > in an e-mail address, then set the target group to "something-X-other". > We want to do it with regex "\\(X\\)" against "userX@valid.domain" and > target group "something-\\1-other". Does that make sense? Basically we > want \1 but without the context of that original string we matched: I think I'm beginning to understand. First, you don't want (replace-match "[first greek letter \\1]" nil nil string 1) but (replace-match "[first greek letter \\1]" nil nil string) then second, you do get the behavior you want in the particular case where the string-match matched the whole string. So to simulate: > (let ((regex "\\(alpha\\)") > (string "gamma alpha beta")) > (when (string-match regex string) > (our-new-function "found greek letter \\1"))) > -> "found greek letter alpha" you'd want: (let ((regex "\\(alpha\\)") (string "gamma alpha beta")) (when (string-match (concat "\\`.*?\\(?:" regex "\\).*\\'") string) (replace-match "found greek letter \\1" nil nil string))) will do what you want, assuming your strings don't contain newlines, and assuming that providing `string' is not a problem. Of course, such string matching is a bit less optimized (and even less so if you replace "." with "\\(?:.\\|\n\\)" to handle newlines), so it might not be ideal. We could implement this feature efficiently by generalizing the `subexp' argument so that rather than subgroup-number it can also take a special new value `whole-string' which means "not just the whole matched text, but the whole freakin' string". Then you could do: (let ((regex "\\(alpha\\)") (string "gamma alpha beta")) (when (string-match regex string) (replace-match "found greek letter \\1" nil nil string 'whole-string))) -- Stefan