From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Klaus Berndl Newsgroups: gmane.emacs.help Subject: Re: I can't believe: replace regexp in a string Date: 14 Feb 2003 10:04:08 +0100 Organization: sd&m AG, Muenchen, Germany Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: <62ede926.0302140037.4db6c80a@posting.google.com> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: main.gmane.org 1045213829 14350 80.91.224.249 (14 Feb 2003 09:10:29 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Fri, 14 Feb 2003 09:10:29 +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 18jbrU-0003j9-00 for ; Fri, 14 Feb 2003 10:10:25 +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 18jbrz-0003I5-07 for gnu-help-gnu-emacs@m.gmane.org; Fri, 14 Feb 2003 04:10:55 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news-han1.dfn.de!news-koe1.dfn.de!lnewspeer01.lnd.ops.eu.uu.net!emea.uu.net!news.sdm.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 58 Original-NNTP-Posting-Host: sachrang.muc.sdm.de Original-X-Trace: solti3.muc.sdm.de 1045213446 28229 193.102.182.12 (14 Feb 2003 09:04:06 GMT) Original-X-Complaints-To: usenet@news.sdm.de Original-NNTP-Posting-Date: 14 Feb 2003 09:04:06 GMT User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Original-Xref: shelby.stanford.edu gnu.emacs.help:110175 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:6677 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:6677 On 14 Feb 2003, Stefan Kamphausen wrote: > Dear elispWizards, > > somehow I can't believe it. When I try to replace a regexp in a string > I have to create a temporary buffer and use the usual buffer > replacement commands or write a loop over the sequence. Is that true? > I have found a posting in this group describing that and have looked > at the XEmacs sources of subr.el where a defun replace-in-string is > defined which does one of the above procedures depending on the length > of the string. Having coded in Perl a good deal this seems quite > strange to me, but if it is the elispish way to do it, I'll give in > :-) There is no need to believe this because elisp can also replace regexps in strings - even without temp. buffers. In general a combination of string-match and replace-match does the trick, see the doc-strings; both of them can get a STRING argument. For replacing all occurences of a regexp in a string a while loop with string-match (the condition) and replace-match (the body) is sufficient. I have already written a function: (defun re-replace-all-string (regexp replacement string &optional regreg start end fixedcase literal subexp) "Replaces in STRING REGEXP with REPLACEMENT between START and END \(or - if not nil - within the substring specified by REGREG) and returns a list containing: 1) the new string 2) the start (usefull if substring is defined by REGREG) 3) the - possible - new END 4) the number of replacements made. Returns nil if STRING is nil. For START and END nil means begin of string (end of string). For the arguments REPLACEMENT, FIXEDCASE, LITERAL and SUBEXP look at `replace-match' with one extension: If REPLACEMENT is exactly `*' then the matching is overwritten with a string of same length consisting of . If REPLACEMENT is exactly `\\*' then the REPLACEMANT will be interpreted as really '`*', i.e. a character followed by a star. This extension is only active if LITERAL is nil!" ;; here comes the code ) If you want i can post it... Ciao, Klaus -- Klaus Berndl mailto: klaus.berndl@sdm.de sd&m AG http://www.sdm.de software design & management Thomas-Dehler-Str. 27, 81737 München, Germany Tel +49 89 63812-392, Fax -220