help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: capitalize string using regular expressions


From: Pascal Bourguignon
Subject: Re: capitalize string using regular expressions
Date: 06 Dec 2003 05:29:17 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

"colin smith" <address@hidden> 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: address@hidden                 ******************


reply via email to

[Prev in Thread] Current Thread [Next in Thread]