From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: lawrence mitchell Newsgroups: gmane.emacs.help Subject: Re: string-replace question -- changing names like variable_name to variableName Date: Tue, 03 Sep 2002 15:39:40 +0100 Organization: funfunfun Sender: help-gnu-emacs-admin@gnu.org Message-ID: References: <3D74B686.1D603DC1@example.net> NNTP-Posting-Host: localhost.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1031064877 25406 127.0.0.1 (3 Sep 2002 14:54:37 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Tue, 3 Sep 2002 14:54:37 +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 17mF4a-0006bQ-00 for ; Tue, 03 Sep 2002 16:54:32 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10) id 17mF67-00008r-00; Tue, 03 Sep 2002 10:56:07 -0400 Original-Path: shelby.stanford.edu!nntp.stanford.edu!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!feed.news.nacamar.de!fu-berlin.de!uni-berlin.de!m22-mp1.cvx1-b.swa.dial.ntli.NET!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 68 Original-NNTP-Posting-Host: m22-mp1.cvx1-b.swa.dial.ntli.net (213.105.232.22) Original-X-Trace: fu-berlin.de 1031064146 56802223 213.105.232.22 (16 [97657]) X-No-Yes: No Mail-Copies-To: nobody User-Agent: Gnus/5.090007 (Oort Gnus v0.07) Emacs/21.2.90 (i386-mingw-windows98.2222) Cancel-Lock: sha1:NhzmnLog4pqkcxL4UaF9pAOsgl4= Original-Xref: nntp.stanford.edu gnu.emacs.help:104429 Original-To: help-gnu-emacs@gnu.org Errors-To: help-gnu-emacs-admin@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.0.11 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: Xref: main.gmane.org gmane.emacs.help:994 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:994 bc wrote: [...] variable_name --> variableName > Is there an easier way to do this short of some lisp like: > (replace-string "_a" "A") > (replace-string "_A" "A") > (replace-string "_b" "B") > etc. Why yes there is. Using a small amount of knowledge of regexps, and a few other Emacs functions. Note firstly, that the docstring of replace-string explicitly recommends against its use in lisp programs: /----[ C-h f replace-string RET ] | This function is usually the wrong thing to use in a Lisp program. | What you probably want is a loop like this: | (while (search-forward FROM-STRING nil t) | (replace-match TO-STRING nil t)) | which will run faster and will not set the mark or print anything. \---- However, neither replace-string nor search-forward allow the use of regexps in their syntax, hence, we need to use the regexp equivalents, namely re-search-forward Using these bits of information, we come up with something like: (let ((case-fold-search nil)) ; make sure we don't ignore ; case in our search, if this ; isn't a problem, then change ; the nil on this line to t. (while (re-search-forward ; search forward for a regular ; expression. "\\([a-z]\\)_\\([a-z]\\)" ; this regexp matches ; something of the form: ; foo_bar, but not _bar, or ; foo_. If you want to match ; capitalised letters add A-Z ; to the character ; groupings. Or change the ; binding of case-fold-search ; to t. nil ; no bound on the search t) ; don't error if the match is ; not found. (replace-match ; replace the matched text with: (concat (match-string 1) ; a concatenation of the 1st ; \\(..\\) grouping. and... (upcase ; the uppercase equivalent of: (match-string 2))) ; the 2nd \\(..\\) grouping. t))) ; since we've gone to all the ; trouble of changing the ; case, treat our string as FIXEDCASE I hope the comments are suitably explanatory. -- lawrence mitchell