From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: kai.grossjohann@uni-duisburg.de (Kai =?iso-8859-1?q?Gro=DFjohann?=) Newsgroups: gmane.emacs.help Subject: Re: I can't believe: replace regexp in a string Date: Sat, 15 Feb 2003 18:48:03 +0100 Organization: University of Duisburg, Germany Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: <848ywhv3jg.fsf@lucy.is.informatik.uni-duisburg.de> References: <62ede926.0302140037.4db6c80a@posting.google.com> <5lheb6zwkj.fsf@rum.cs.yale.edu> <62ede926.0302150707.66866ad6@posting.google.com> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1045331469 3971 80.91.224.249 (15 Feb 2003 17:51:09 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Sat, 15 Feb 2003 17:51:09 +0000 (UTC) Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 18k6Sx-00011u-00 for ; Sat, 15 Feb 2003 18:51:08 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 18k6So-0005Th-08 for gnu-help-gnu-emacs@m.gmane.org; Sat, 15 Feb 2003 12:50:58 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!syros.belnet.be!news.belnet.be!uni-erlangen.de!fu-berlin.de!uni-berlin.de!pd9e1ec25.dip.t-dialin.NET!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 45 Original-NNTP-Posting-Host: pd9e1ec25.dip.t-dialin.net (217.225.236.37) Original-X-Trace: fu-berlin.de 1045331326 47578406 217.225.236.37 (16 [73968]) User-Agent: Gnus/5.090016 (Oort Gnus v0.16) Emacs/21.3.50 Cancel-Lock: sha1:fTqBl3PtarUbUMFeE2bMo9qLJ4c= Original-Xref: shelby.stanford.edu gnu.emacs.help:110236 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:6739 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:6739 kamphausen@creativepharma.com (Stefan Kamphausen) writes: > "Stefan Monnier " 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