From: kai.grossjohann@uni-duisburg.de (Kai Großjohann)
Subject: Re: I can't believe: replace regexp in a string
Date: Sat, 15 Feb 2003 18:48:03 +0100 [thread overview]
Message-ID: <848ywhv3jg.fsf@lucy.is.informatik.uni-duisburg.de> (raw)
In-Reply-To: 62ede926.0302150707.66866ad6@posting.google.com
kamphausen@creativepharma.com (Stefan Kamphausen) writes:
> "Stefan Monnier <foo@acm.com>" <monnier+gnu.emacs.help/news/@flint.cs.yale.edu> wrote in message news:<5lheb6zwkj.fsf@rum.cs.yale.edu>...
>>
>> 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
next prev parent reply other threads:[~2003-02-15 17:48 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-02-14 8:37 I can't believe: replace regexp in a string Stefan Kamphausen
2003-02-14 9:04 ` Klaus Berndl
2003-02-14 9:11 ` David Kastrup
2003-02-14 15:55 ` Stefan Monnier <foo@acm.com>
2003-02-15 15:07 ` Stefan Kamphausen
2003-02-15 17:48 ` Kai Großjohann [this message]
2003-02-17 11:24 ` Stefan Kamphausen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=848ywhv3jg.fsf@lucy.is.informatik.uni-duisburg.de \
--to=kai.grossjohann@uni-duisburg.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.