[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Utilizing Regexp (or something else) to replace an arbitrary string
From: |
tyler |
Subject: |
Re: Utilizing Regexp (or something else) to replace an arbitrary string length of the same character with the same string length of another character. |
Date: |
Thu, 27 Nov 2008 21:01:26 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux) |
"Tim Visher" <tim.visher@gmail.com> writes:
> Yeah, I did consider using replace-string and query-replace-string and
> their ilk. The issue is that I use the characters I want to replace
> at other locations where I don't want them replaced. Also, I want to
> replace an arbitrary amount of them at one time. I actually already
> did the edits that I want done by using query-replace-string.
> However, if the document had been much longer, that really would've
> taken too long. It doesn't sound like there's a quick way to do this
> in Emacs. Oh well.
>
This brings to mind the old chestnut:
Some people, when confronted with a problem, think "I know, I’ll use
regular expressions." Now they have two problems. --Jamie Zawinski
Evaluate the following (C-x C-e with point after the final ')' ), then
call it with `M-x switcheroo' when point is before the sample text below.
It solves your problem, as I understand it.
(defun switcheroo ()
"Replaces multiple consecutive '=' chars with the same number of '*'.
Single '=' are left unmolested"
(interactive)
(save-excursion
(while (search-forward "=")
(if (looking-at "=")
(progn (backward-char 1)
(while (looking-at "=")
(delete-char 1)
(insert "*")))))))
==========asdf;lkj asdf= asdflkj = asdf
asflkj ==asdfa;lkj
aslkj ===as;fl;kj =====asd;flkj
as;dlfkja sd=============
asdf=asdflkj=asdfas=
=asdf=asdf=
==========================
HTH,
Tyler
--
There is something fascinating about science. One gets such wholesale
returns of conjecture out of such a trifling investment of fact.
--Mark Twain