* capitalize string using regular expressions
@ 2003-12-06 0:53 colin smith
2003-12-06 2:19 ` Tim Heaney
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: colin smith @ 2003-12-06 0:53 UTC (permalink / raw)
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.
Thanks a lot,
Colin.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: capitalize string using regular expressions
2003-12-06 0:53 capitalize string using regular expressions colin smith
@ 2003-12-06 2:19 ` Tim Heaney
2003-12-06 3:29 ` Greg Hill
2003-12-06 4:29 ` Pascal Bourguignon
2 siblings, 0 replies; 4+ messages in thread
From: Tim Heaney @ 2003-12-06 2:19 UTC (permalink / raw)
"colin smith" <zxcv4856@hotmail.com> writes:
>
> 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.
Hm. I couldn't quite figure it out either. Here's how you might do it
in Perl
perl -i~ -pe 's/\b(\w+)(\.txt)\b/uc($1).$2/ge' file
That is, if that's run from the command line then file will be edited
and file~ will be the original. In emacs, I don't know how to
uppercase part of the search string like that. I guess you could
define a macro to do it:
C-x ( ; start recording macro
C-M-s ; start regular expression search
\<[a-z]+\.txt\> ; regular expression to search for
M-4 C-b ; move back 4 places (the .txt)
M-- M-u ; uppercase previous word
C-x ) ; stop recording macro
Then to execute the macro, keep hitting
C-x e
as many times as you need. I dunno. Perhaps someone else will post a
more straightforward way to do it.
Tim
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: capitalize string using regular expressions
2003-12-06 0:53 capitalize string using regular expressions colin smith
2003-12-06 2:19 ` Tim Heaney
@ 2003-12-06 3:29 ` Greg Hill
2003-12-06 4:29 ` Pascal Bourguignon
2 siblings, 0 replies; 4+ messages in thread
From: Greg Hill @ 2003-12-06 3:29 UTC (permalink / raw)
At 12:53 AM +0000 12/6/03, colin smith wrote:
> 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.
(while (re-search-forward "\\W\\(\\w+\\)\\.txt\\W" nil t)
(replace-match (upcase (match-string-no-properties 1))
nil nil nil 1)))
--Greg
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: capitalize string using regular expressions
2003-12-06 0:53 capitalize string using regular expressions colin smith
2003-12-06 2:19 ` Tim Heaney
2003-12-06 3:29 ` Greg Hill
@ 2003-12-06 4:29 ` Pascal Bourguignon
2 siblings, 0 replies; 4+ messages in thread
From: Pascal Bourguignon @ 2003-12-06 4:29 UTC (permalink / raw)
"colin smith" <zxcv4856@hotmail.com> 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 ******************
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2003-12-06 4:29 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-12-06 0:53 capitalize string using regular expressions colin smith
2003-12-06 2:19 ` Tim Heaney
2003-12-06 3:29 ` Greg Hill
2003-12-06 4:29 ` Pascal Bourguignon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).