[Top][All Lists]

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

Re: I can't believe: replace regexp in a string

From: Kai Großjohann
Subject: Re: I can't believe: replace regexp in a string
Date: Sat, 15 Feb 2003 18:48:03 +0100
User-agent: Gnus/5.090016 (Oort Gnus v0.16) Emacs/21.3.50 (Stefan Kamphausen) writes:

> "Stefan Monnier <>" 
> <> wrote in message 
> news:<>...
>> I'm not sure what's your complaint exactly.
>> Is it that XEmacs' replace-in-string is not standard in Emacs (this
>> is addressed in Emacs-21 with replace-regexp-in-string) or is it
>> that you find both implementations of replace-in-string inefficient
>> (how is it implemented in Perl) ?
> I'm just wondering that such a trivial task (if your used to "$string
> =~ s/match-re/replace-re/" in Perl) like doing a match and replace on
> a string seems to be really non-trivial in Emacs, and I'm speaking of
> both Emacsen here. I thought I must have missed something somewhere.

Well, operations in a buffer are simpler, perhaps.

>> Finally, the reason why it took so long for Emacs to provide
>> replace-regexp-in-string is because Emacs uses buffers a lot more than
>> strings, so if you need replace-regexp-in-string it's maybe because you
>> made the arguably wrong decision to use a string rather than a buffer.
> Hm, I'm parsing a text file and need to replace some trailing
> whitespaces from a substring I just read. You're right in that I could
> do that replacement in the buffer I use for reading the files'
> contents (and thinking about that right now I might just do it that
> way ;-). Thanks for that hint.

Why does the substring contain the spaces in the first place?

Generally, when parsing a text file it is often better to use general
movement functions rather than to rely on regular expressions.
(Sometimes, regexes are the right tool even in Emacs.  But in Perl, a
regex is ALWAYS the right solution, in Emacs it SELDOM is.)

For example, you could frob the syntax correctly and then what you're
looking for might be a word, or a string, or an s-expression.

In your specific case, if you use searching in the buffer to find the
right spot, then you can use skip-syntax-backward or
skip-chars-backward to skip backwards over the trailing spaces.
Problem solved :-)

A turnip curses Elvis

reply via email to

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