From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Pascal Bourguignon Newsgroups: gmane.emacs.help Subject: Re: capitalize string using regular expressions Date: 06 Dec 2003 05:29:17 +0100 Organization: [posted via Easynet Spain] Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: <87ptf2a7s2.fsf@thalassa.informatimago.com> References: <3M9Ab.324$vg4.207@nwrdny02.gnilink.net> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1070685874 13484 80.91.224.253 (6 Dec 2003 04:44:34 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 6 Dec 2003 04:44:34 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Dec 06 05:44:32 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1ASUIx-0000i1-00 for ; Sat, 06 Dec 2003 05:44:31 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1ASVGF-0006pL-Ur for geh-help-gnu-emacs@m.gmane.org; Sat, 06 Dec 2003 00:45:47 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!newsmi-us.news.garr.it!NewsITBone-GARR!news.mailgate.org!newsfeeder.edisontel.com!tiscali!newsfeed1.ip.tiscali.net!feed.news.tiscali.de!easynet-monga!easynet.net!easynet-post2!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 Original-Lines: 87 Original-NNTP-Posting-Host: 62.93.174.79 Original-X-Trace: DXC=?RUgmGAZ98Pko0MMFVEW@M2bjd]fUbBd4DjF1d]Q Original-Xref: shelby.stanford.edu gnu.emacs.help:119057 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:14997 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:14997 "colin smith" writes: > Hi Everyone, > > Does anyone know how I can replace all occurences of any strings that > end with ".txt" (lets say "ghjkl.txt" and "xyz.txt") with their uppercase > equivalents(ie. "GHJKL.txt" and "XYZ.txt"). There must be some way of doing > this using regular expressions. I've looked extensively through the emacs > help, but havent been able to figure it out. Your problem is slightly ill-defined. What are those "any strings" you're speaking about? Imagine a buffer containing: toto.txt titi.txt tutu.txt tata.txt Do you want: TOTO.TXT TITI.TXT TUTU.txt TATA.txt or do you want: TOTO.txt TITI.txt TUTU.txt TATA.txt What about: txt.txt.txt.txt tu\ tu.txt ? (goto-char (point-min)) (let ((case-fold-search nil)) (while (re-search-forward "\\(.+\\)\\(\\.txt\\)" nil t) (let ((before (match-string 1))) (delete-region (match-beginning 1) (match-end 1)) (goto-char (match-beginning 1)) (insert before)))) You can run it on a given buffer with: M-x eval-expression RET and pasting the expression and RET, or you can make a command: (defun upcase-before-.txt (start end) (interactive "r") (goto-char start) (let ((case-fold-search nil)) (while (re-search-forward "\\(.+\\)\\(\\.txt\\)" end t) ;; note implicit ^ beginning and ^ end of anything ;; on the same line! ("." does match anything but a new line). ;; Was it what you meant? (let ((before (upcase (match-string 1)))) (delete-region (match-beginning 1) (match-end 1)) (goto-char (match-beginning 1)) (insert before))))) and select a region and M-x upcase-before-.txt RET If you mean individual strings, what do you mean by "all occurrences"? (show (mapcar (lambda (any-string) (let ((case-fold-search nil)) (if (string-match "^\\(.*\\)\\.txt$" any-string) ;; note explicit ^ begin and $ end of string ! (concat (upcase (match-string 1 any-string)) ".txt") any-string))) '("toto.txt" "titi" "tata.txt" "tutu.TXT" "txt.txt.txt" "toto.txt tutu.txt")) ) ==> ("TOTO.txt" "titi" "TATA.txt" "tutu.TXT" "TXT.TXT.txt" "TOTO.TXT TUTU.txt") -- __Pascal_Bourguignon__ . * * . * .* . http://www.informatimago.com/ . * . .* * . . /\ ( . . * Living free in Alaska or in Siberia, a . . / .\ . * . grizzli's life expectancy is 35 years, .*. / * \ . . but no more than 8 years in captivity. . /* o \ . http://www.theadvocates.org/ * '''||''' . SCO Spam-magnet: postmaster@sco.com ******************