GNU bug report logs - #12622
replace-regexp-in-string

Previous Next

Package: emacs;

Reported by: Andreas Röhler <andreas.roehler <at> easy-emacs.de>

Date: Thu, 11 Oct 2012 14:19:02 UTC

Severity: normal

Tags: notabug

Done: Glenn Morris <rgm <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 12622 in the body.
You can then email your comments to 12622 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#12622; Package emacs. (Thu, 11 Oct 2012 14:19:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Andreas Röhler <andreas.roehler <at> easy-emacs.de>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Thu, 11 Oct 2012 14:19:03 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Andreas Röhler <andreas.roehler <at> easy-emacs.de>
To: bug-gnu-emacs <at> gnu.org
Subject: replace-regexp-in-string
Date: Thu, 11 Oct 2012 16:17:24 +0200
Hi,

the following question was raised at:

http://stackoverflow.com/questions/12809610/replace-regexp-in-string-with-lookahead-behind

I'd like to replace all occurrences of \w \w with \w\\\w in a string, where the \w parts stay the same before and after the replacement, e.g.

[.A foobar] [.B baz]

should result in

[.A\\foobar] [.B\\baz]

;;;;;;;;;;

this might be solved in buffer

by

(while  (re-search-forward "\\_<\\w+\\_>" nil t 1)
  (replace-match (concat "\\\\\\\\" (match-string-no-properties 0) )))

Also would expect a respective form working with replace-regexp-in-string

(setq mystring "[.A foobar] [.B baz]")
(replace-regexp-in-string  "\\_<\\w+\\_>" (concat "\\\\" (match-string-no-properties 0) )   mystring)

but it fails.

When commenting "(match-string 0 str)" as follows it subr.el it works

	(setq matches
	      (cons (replace-match (if (stringp rep)
				       rep
				     (funcall rep
                                              ;; (match-string 0 str)
))
				   fixedcase literal str subexp)

Which has some plausibility: when receiving a user-specified function, there must not be that argument.

Maybe have a look,

Best,

Andreas







Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#12622; Package emacs. (Thu, 11 Oct 2012 14:45:02 GMT) Full text and rfc822 format available.

Message #8 received at 12622 <at> debbugs.gnu.org (full text, mbox):

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: Andreas Röhler <andreas.roehler <at> easy-emacs.de>
Cc: 12622 <at> debbugs.gnu.org
Subject: Re: bug#12622: replace-regexp-in-string
Date: Thu, 11 Oct 2012 16:44:03 +0200
Andreas Röhler <andreas.roehler <at> easy-emacs.de> writes:

> Also would expect a respective form working with replace-regexp-in-string
>
> (setq mystring "[.A foobar] [.B baz]")
> (replace-regexp-in-string  "\\_<\\w+\\_>" (concat "\\\\" (match-string-no-properties 0) )   mystring)
>
> but it fails.

Fails in which way?  What do you expect, and what do you get?

> When commenting "(match-string 0 str)" as follows it subr.el it works
>
> 	(setq matches
> 	      (cons (replace-match (if (stringp rep)
> 				       rep
> 				     (funcall rep
>                                               ;; (match-string 0 str)
> ))
> 				   fixedcase literal str subexp)
>
> Which has some plausibility: when receiving a user-specified function, there must not be that argument.

Why?  That's what the doc string documents.

Andreas.

-- 
Andreas Schwab, schwab <at> linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#12622; Package emacs. (Thu, 11 Oct 2012 16:40:02 GMT) Full text and rfc822 format available.

Message #11 received at 12622 <at> debbugs.gnu.org (full text, mbox):

From: Andreas Röhler <andreas.roehler <at> easy-emacs.de>
To: Andreas Schwab <schwab <at> linux-m68k.org>
Cc: 12622 <at> debbugs.gnu.org
Subject: Re: bug#12622: replace-regexp-in-string
Date: Thu, 11 Oct 2012 18:38:00 +0200
Am 11.10.2012 16:44, schrieb Andreas Schwab:
> Andreas Röhler <andreas.roehler <at> easy-emacs.de> writes:
>
>> Also would expect a respective form working with replace-regexp-in-string
>>
>> (setq mystring "[.A foobar] [.B baz]")
>> (replace-regexp-in-string  "\\_<\\w+\\_>" (concat "\\\\" (match-string-no-properties 0) )   mystring)
>>
>> but it fails.
>
> Fails in which way?  What do you expect, and what do you get?
>
>> When commenting "(match-string 0 str)" as follows it subr.el it works
>>
>> 	(setq matches
>> 	      (cons (replace-match (if (stringp rep)
>> 				       rep
>> 				     (funcall rep
>>                                                ;; (match-string 0 str)
>> ))
>> 				   fixedcase literal str subexp)
>>
>> Which has some plausibility: when receiving a user-specified function, there must not be that argument.
>
> Why?  That's what the doc string documents.
>
> Andreas.
>


(setq mystring "[.A foobar] [.B baz]")

;; works nice in buffer
(while  (re-search-forward "\\_<\\w+\\_>" nil t 1)
  (replace-match (concat "\\\\\\\\" (match-string-no-properties 0) )))

==>
[.A \\foobar] [.B \\baz]

has unpredictable results

(setq mystring "[.A foobar] [.B baz]")
(replace-regexp-in-string "\\_<\\w+\\_>" (concat "\\\\" (match-string-no-properties 0)) mystring) ==>

 "[.A \\ext in that fi] [.B \\ext in that fi]"

for example,

Docstring says:

"If it is a function, it is called with the actual text of each
match, and its value is used as the replacement text."

Which is not the case AFAIS.







Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#12622; Package emacs. (Thu, 11 Oct 2012 16:54:02 GMT) Full text and rfc822 format available.

Message #14 received at 12622 <at> debbugs.gnu.org (full text, mbox):

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: Andreas Röhler <andreas.roehler <at> easy-emacs.de>
Cc: 12622 <at> debbugs.gnu.org
Subject: Re: bug#12622: replace-regexp-in-string
Date: Thu, 11 Oct 2012 18:52:46 +0200
Andreas Röhler <andreas.roehler <at> easy-emacs.de> writes:

> (setq mystring "[.A foobar] [.B baz]")
> (replace-regexp-in-string "\\_<\\w+\\_>" (concat "\\\\" (match-string-no-properties 0)) mystring) ==>
>
>  "[.A \\ext in that fi] [.B \\ext in that fi]"
>
> for example,
>
> Docstring says:
>
> "If it is a function, it is called with the actual text of each
> match, and its value is used as the replacement text."
>
> Which is not the case AFAIS.

You are not passing a function.

Andreas.

-- 
Andreas Schwab, schwab <at> linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#12622; Package emacs. (Fri, 12 Oct 2012 06:34:01 GMT) Full text and rfc822 format available.

Message #17 received at 12622 <at> debbugs.gnu.org (full text, mbox):

From: Andreas Röhler <andreas.roehler <at> easy-emacs.de>
To: Andreas Schwab <schwab <at> linux-m68k.org>
Cc: 12622 <at> debbugs.gnu.org
Subject: Re: bug#12622: replace-regexp-in-string
Date: Fri, 12 Oct 2012 08:32:39 +0200
Am 11.10.2012 18:52, schrieb Andreas Schwab:
> Andreas Röhler <andreas.roehler <at> easy-emacs.de> writes:
>
>> (setq mystring "[.A foobar] [.B baz]")
>> (replace-regexp-in-string "\\_<\\w+\\_>" (concat "\\\\" (match-string-no-properties 0)) mystring) ==>
>>
>>   "[.A \\ext in that fi] [.B \\ext in that fi]"
>>
>> for example,
>>
>> Docstring says:
>>
>> "If it is a function, it is called with the actual text of each
>> match, and its value is used as the replacement text."
>>
>> Which is not the case AFAIS.
>
> You are not passing a function.
>
> Andreas.
>

okay.

that works:

(replace-regexp-in-string "\\_<\\w+\\_>" (lambda (x) (concat "\\\\" x)) mystring)

Nonetheless consider it a design flaw, us it requires a argument taking function, where a expression just to evaluate would suffice.

Andreas





Added tag(s) notabug. Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Fri, 12 Oct 2012 06:36:01 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 12622 <at> debbugs.gnu.org and Andreas Röhler <andreas.roehler <at> easy-emacs.de> Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Fri, 12 Oct 2012 06:36:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#12622; Package emacs. (Fri, 12 Oct 2012 08:16:01 GMT) Full text and rfc822 format available.

Message #24 received at 12622-done <at> debbugs.gnu.org (full text, mbox):

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: 12622-done <at> debbugs.gnu.org
Subject: Re: bug#12622: replace-regexp-in-string
Date: Fri, 12 Oct 2012 10:14:45 +0200
Not a bug.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#12622; Package emacs. (Fri, 12 Oct 2012 14:24:01 GMT) Full text and rfc822 format available.

Message #27 received at 12622 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Andreas Röhler <andreas.roehler <at> easy-emacs.de>
Cc: 12622 <at> debbugs.gnu.org, Andreas Schwab <schwab <at> linux-m68k.org>
Subject: Re: bug#12622: replace-regexp-in-string
Date: Fri, 12 Oct 2012 10:22:06 -0400
> Nonetheless consider it a design flaw, us it requires a argument taking
> function, where a expression just to evaluate would suffice.

You need some way to distinguish "here's an expression that will return
the string that needs to be passed as argument" from "here's the
expression that needs to be passed as argument, to be evaluated later in
each iteration of the loop".  The distinction is made by wrapping the
expression in a `lambda'.


        Stefan




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#12622; Package emacs. (Fri, 12 Oct 2012 16:09:01 GMT) Full text and rfc822 format available.

Message #30 received at 12622 <at> debbugs.gnu.org (full text, mbox):

From: Andreas Röhler <andreas.roehler <at> easy-emacs.de>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: 12622 <at> debbugs.gnu.org, Andreas Schwab <schwab <at> linux-m68k.org>
Subject: Re: bug#12622: replace-regexp-in-string
Date: Fri, 12 Oct 2012 18:07:30 +0200
Am 12.10.2012 16:22, schrieb Stefan Monnier:
>> Nonetheless consider it a design flaw, us it requires a argument taking
>> function, where a expression just to evaluate would suffice.
>
> You need some way to distinguish "here's an expression that will return
> the string that needs to be passed as argument" from "here's the
> expression that needs to be passed as argument, to be evaluated later in
> each iteration of the loop".  The distinction is made by wrapping the
> expression in a `lambda'.
>
>
>          Stefan
>

will keep that under my pillow :)

back to the question:

what about writing here instead

 (funcall rep (match-string 0 str)))

just

(eval rep)

i.e. let the user decide how to construct, if not delivered a regexp as string

Best,

Andreas




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#12622; Package emacs. (Fri, 12 Oct 2012 18:14:02 GMT) Full text and rfc822 format available.

Message #33 received at 12622 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> IRO.UMontreal.CA>
To: Andreas Röhler <andreas.roehler <at> easy-emacs.de>
Cc: 12622 <at> debbugs.gnu.org, Andreas Schwab <schwab <at> linux-m68k.org>
Subject: Re: bug#12622: replace-regexp-in-string
Date: Fri, 12 Oct 2012 14:12:34 -0400
> (eval rep)

`eval' is evil and should be avoided when possible (typically replaced
by `funcall').

Of course, in this case, `rep' is always a value anyway, because all the
arguments of a call are always evaluated before entering the body of the
function (at least in Elisp, which uses "eager" or "strict" or
"applicative order" evaluation) so there's nothing left to evaluate.


        Stefan




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sat, 10 Nov 2012 12:24:03 GMT) Full text and rfc822 format available.

This bug report was last modified 12 years and 224 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.