help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [2nd attempt] What's the doc of replace-regexp-in-string saying ?


From: Orivej Desh
Subject: Re: [2nd attempt] What's the doc of replace-regexp-in-string saying ?
Date: Wed, 08 Dec 2010 15:37:10 -0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

Bill Marcum <bill@lat.localnet> writes:

> On 2010-11-22, bolega <gnuist006@gmail.com> wrote:
>>
>> On Nov 21, 3:27 pm, bolega <gnuist...@gmail.com> wrote:
>>> Hello All,
>>>
>>> In this doc, its not clear what the role of \' is ?
>>>
>>>
>>> To replace only the first match (if any), make REGEXP match up to \'
>>> and replace a sub-expression, e.g.
>>>   (replace-regexp-in-string "\(foo\).*\'" "bar" " foo foo" nil nil 1)
>>>     => " bar foo"
>>
> This is off topic for comp.unix.shell, but it seems clear to me. If the 
> regexp didn't end with \', it would change " foo foo" to " bar bar".

Since matching is hungry, this is not the case:

     (replace-regexp-in-string "\\(foo\\).*\\'" "bar"
                               " foo foo" nil nil 1)
     " bar foo"

     (replace-regexp-in-string "\\(foo\\).*" "bar"
                               " foo foo" nil nil 1)
     " bar foo"

Moreover, I'm sure bolega has spotted an error in documentation.
Consider this:

     (replace-regexp-in-string "\\(foo\\).*\\'" "bar"
                                "foo foo\n foo foo" nil nil 1)
     "foo foo
      bar foo"

Documentation string assumed that `.' would match a newline.

\' matches an end of a string, unlike $ which matches both an end of a
string and an end of a line).  Without \' or with $ the last regexp
matches twice:

     (replace-regexp-in-string "\\(foo\\).*" "bar"
                                "foo foo\n foo foo" nil nil 1)
     "bar foo
      bar foo"

     (replace-regexp-in-string "\\(foo\\).*$" "bar"
                                "foo foo\n foo foo" nil nil 1)
     "bar foo
      bar foo"


reply via email to

[Prev in Thread] Current Thread [Next in Thread]