unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Ted Zlatanov <tzz@lifelogs.com>
Cc: ding@gnus.org, emacs-devel@gnu.org
Subject: Re: replace matches in any string
Date: Thu, 02 Sep 2010 18:21:22 +0200	[thread overview]
Message-ID: <jwvaao06sll.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <87fwxs5m0h.fsf@lifelogs.com> (Ted Zlatanov's message of "Thu, 02 Sep 2010 08:10:38 -0500")

> (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



  parent reply	other threads:[~2010-09-02 16:21 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <87ab3gfb41.fsf@hati.baby-gnu.org>
     [not found] ` <87r5wrgg8w.fsf@lifelogs.com>
     [not found]   ` <87y6gvwryp.fsf@hati.baby-gnu.org>
     [not found]     ` <87d3y7p1ie.fsf@gnu.org>
     [not found]       ` <87633y7v02.fsf@hati.baby-gnu.org>
     [not found]         ` <m2aatadtda.fsf@igel.home>
     [not found]           ` <87vdbyccgg.fsf@hati.baby-gnu.org>
     [not found]             ` <m21vemdo67.fsf@igel.home>
     [not found]               ` <87eiikdfub.fsf@hati.baby-gnu.org>
     [not found]                 ` <87ocf18ntz.fsf@hati.baby-gnu.org>
2010-07-08 13:57                   ` [PATCH] Use replace-match in posting-style Ted Zlatanov
2010-07-12 19:43                     ` Daniel Dehennin
2010-07-30 17:43                       ` replace matches in any string (was: [PATCH] Use replace-match in posting-style.) Ted Zlatanov
2010-09-01 13:55                         ` replace matches in any string Ted Zlatanov
2010-09-02 11:29                           ` Stefan Monnier
2010-09-02 13:10                             ` Ted Zlatanov
2010-09-02 14:44                               ` Lars Magne Ingebrigtsen
2010-09-02 16:21                               ` Stefan Monnier [this message]
2010-09-02 16:45                                 ` David Kastrup
2010-09-02 17:08                                 ` Ted Zlatanov
2010-09-02 17:22                                   ` David Kastrup
2010-09-02 17:51                                     ` Ted Zlatanov
2010-09-02 18:04                                       ` David Kastrup
2010-09-02 19:12                                         ` Ted Zlatanov
2010-09-02 19:17                                           ` Recommended gnus spam filter system? Camm Maguire
2010-09-02 19:42                                             ` Thorsten Bonow
2010-09-02 20:31                                             ` Frank Schmitt
2010-09-02 20:38                                             ` Karl Fogel
2010-09-02 20:27                                     ` replace matches in any string Stefan Monnier
2010-09-02 22:18                                       ` Lars Magne Ingebrigtsen
2010-09-02 22:40                                         ` Davis Herring
2010-09-03  5:33                                       ` David Kastrup
2010-09-03 17:06                                         ` Lars Magne Ingebrigtsen

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=jwvaao06sll.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=ding@gnus.org \
    --cc=emacs-devel@gnu.org \
    --cc=tzz@lifelogs.com \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).