Re: replace-regexp-in-string, using the dyn scoped variable STR

From:

Stefan Monnier

Subject:

Re: replace-regexp-in-string, using the dyn scoped variable STR

Date:

Thu, 12 Nov 2009 10:26:35 -0500

User-agent:

Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

>> (setq org-str (replace-regexp-in-string
>> "&#x\\([a-f0-9]\\{2,4\\}\\);"
>> (lambda (m)
>> (char-to-string
>> ;; Note: str is scoped dynamically from
>> ;; `replace-regexp-in-string'.
>> (+ (string-to-number (match-string 1 str) 16)
> `str' has the same value as `m'.
No, it doesn't: str is "the whole string" whereas m is only the
(match-string 0) part of it (which is why it's more convenient to use
(match-string 1 str) in Lennart's example than to try and figure out
where submatch 1 is in m, which basically requires either doing another
string-match or using ad-hoc methods (e.g. (substring m 3 -1))).
Stefan