* bug#12622: replace-regexp-in-string
@ 2012-10-11 14:17 Andreas Röhler
2012-10-11 14:44 ` Andreas Schwab
0 siblings, 1 reply; 9+ messages in thread
From: Andreas Röhler @ 2012-10-11 14:17 UTC (permalink / raw)
To: 12622
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
^ permalink raw reply [flat|nested] 9+ messages in thread
* bug#12622: replace-regexp-in-string
2012-10-11 14:17 bug#12622: replace-regexp-in-string Andreas Röhler
@ 2012-10-11 14:44 ` Andreas Schwab
2012-10-11 16:38 ` Andreas Röhler
0 siblings, 1 reply; 9+ messages in thread
From: Andreas Schwab @ 2012-10-11 14:44 UTC (permalink / raw)
To: Andreas Röhler; +Cc: 12622
Andreas Röhler <andreas.roehler@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@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 9+ messages in thread
* bug#12622: replace-regexp-in-string
2012-10-11 14:44 ` Andreas Schwab
@ 2012-10-11 16:38 ` Andreas Röhler
2012-10-11 16:52 ` Andreas Schwab
0 siblings, 1 reply; 9+ messages in thread
From: Andreas Röhler @ 2012-10-11 16:38 UTC (permalink / raw)
To: Andreas Schwab; +Cc: 12622
Am 11.10.2012 16:44, schrieb Andreas Schwab:
> Andreas Röhler <andreas.roehler@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.
^ permalink raw reply [flat|nested] 9+ messages in thread
* bug#12622: replace-regexp-in-string
2012-10-11 16:38 ` Andreas Röhler
@ 2012-10-11 16:52 ` Andreas Schwab
2012-10-12 6:32 ` Andreas Röhler
0 siblings, 1 reply; 9+ messages in thread
From: Andreas Schwab @ 2012-10-11 16:52 UTC (permalink / raw)
To: Andreas Röhler; +Cc: 12622
Andreas Röhler <andreas.roehler@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@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 9+ messages in thread
* bug#12622: replace-regexp-in-string
2012-10-11 16:52 ` Andreas Schwab
@ 2012-10-12 6:32 ` Andreas Röhler
2012-10-12 8:14 ` Andreas Schwab
2012-10-12 14:22 ` Stefan Monnier
0 siblings, 2 replies; 9+ messages in thread
From: Andreas Röhler @ 2012-10-12 6:32 UTC (permalink / raw)
To: Andreas Schwab; +Cc: 12622
Am 11.10.2012 18:52, schrieb Andreas Schwab:
> Andreas Röhler <andreas.roehler@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
^ permalink raw reply [flat|nested] 9+ messages in thread
* bug#12622: replace-regexp-in-string
2012-10-12 6:32 ` Andreas Röhler
@ 2012-10-12 8:14 ` Andreas Schwab
2012-10-12 14:22 ` Stefan Monnier
1 sibling, 0 replies; 9+ messages in thread
From: Andreas Schwab @ 2012-10-12 8:14 UTC (permalink / raw)
To: 12622-done
Not a bug.
^ permalink raw reply [flat|nested] 9+ messages in thread
* bug#12622: replace-regexp-in-string
2012-10-12 6:32 ` Andreas Röhler
2012-10-12 8:14 ` Andreas Schwab
@ 2012-10-12 14:22 ` Stefan Monnier
2012-10-12 16:07 ` Andreas Röhler
1 sibling, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2012-10-12 14:22 UTC (permalink / raw)
To: Andreas Röhler; +Cc: 12622, Andreas Schwab
> 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
^ permalink raw reply [flat|nested] 9+ messages in thread
* bug#12622: replace-regexp-in-string
2012-10-12 14:22 ` Stefan Monnier
@ 2012-10-12 16:07 ` Andreas Röhler
2012-10-12 18:12 ` Stefan Monnier
0 siblings, 1 reply; 9+ messages in thread
From: Andreas Röhler @ 2012-10-12 16:07 UTC (permalink / raw)
To: Stefan Monnier; +Cc: 12622, Andreas Schwab
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
^ permalink raw reply [flat|nested] 9+ messages in thread
* bug#12622: replace-regexp-in-string
2012-10-12 16:07 ` Andreas Röhler
@ 2012-10-12 18:12 ` Stefan Monnier
0 siblings, 0 replies; 9+ messages in thread
From: Stefan Monnier @ 2012-10-12 18:12 UTC (permalink / raw)
To: Andreas Röhler; +Cc: 12622, Andreas Schwab
> (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
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2012-10-12 18:12 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-11 14:17 bug#12622: replace-regexp-in-string Andreas Röhler
2012-10-11 14:44 ` Andreas Schwab
2012-10-11 16:38 ` Andreas Röhler
2012-10-11 16:52 ` Andreas Schwab
2012-10-12 6:32 ` Andreas Röhler
2012-10-12 8:14 ` Andreas Schwab
2012-10-12 14:22 ` Stefan Monnier
2012-10-12 16:07 ` Andreas Röhler
2012-10-12 18:12 ` Stefan Monnier
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).