unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Utilizing Regexp (or something else) to replace an arbitrary string length of the same character with the same string length of another character.
@ 2008-11-26 14:41 Tim Visher
  2008-11-26 16:31 ` Peter Dyballa
  0 siblings, 1 reply; 7+ messages in thread
From: Tim Visher @ 2008-11-26 14:41 UTC (permalink / raw
  To: help-gnu-emacs

Hello Everyone,

I have a bunch of arbitrarily long strings of '=' characters and I
would like to replace them one for one with '-' characters.  I know I
could do this with a simple find and replace using isearch, but that
would require me going through them one at a time.  I'm wondering if
there's a way to do this with a regex such as:

    r/(=+)/-{length of n}

Obviously that's a major bastardization of syntax and doesn't really
exist in any language, but I figure this should be able to be done in
Emacs somehow.

Thanks in advance!

-- 

In Christ,

Timmy V.

http://burningones.com/
http://five.sentenc.es/ - Spend less time on e-mail




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Utilizing Regexp (or something else) to replace an arbitrary string length of the same character with the same string length of another character.
  2008-11-26 14:41 Utilizing Regexp (or something else) to replace an arbitrary string length of the same character with the same string length of another character Tim Visher
@ 2008-11-26 16:31 ` Peter Dyballa
  0 siblings, 0 replies; 7+ messages in thread
From: Peter Dyballa @ 2008-11-26 16:31 UTC (permalink / raw
  To: Tim Visher; +Cc: help-gnu-emacs


Am 26.11.2008 um 15:41 schrieb Tim Visher:

> I have a bunch of arbitrarily long strings of '=' characters and I
> would like to replace them one for one with '-' characters.

Have you thought of replace-string? (replace-string from-string to- 
string &optional delimited start end) does not use a regular  
expression, it substitutes one string (=) with another (-).

>
> In Christ,


No! In Emacs.

--
Greetings

   Pete

We are usually convinced more easily by reasons we have found  
ourselves than by those which have occurred to others.
				– Blaise Pascal







^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Utilizing Regexp (or something else) to replace an arbitrary  string length of the same character with the same string length of another character.
       [not found] <mailman.1299.1227715545.26697.help-gnu-emacs@gnu.org>
@ 2008-11-26 17:25 ` Xah Lee
  2008-11-27 20:24   ` Tim Visher
  0 siblings, 1 reply; 7+ messages in thread
From: Xah Lee @ 2008-11-26 17:25 UTC (permalink / raw
  To: help-gnu-emacs

On Nov 26, 6:41 am, "Tim Visher" <tim.vis...@gmail.com> wrote:
> Hello Everyone,
>
> I have a bunch of arbitrarily long strings of '=' characters and I
> would like to replace them one for one with '-' characters.  I know I
> could do this with a simple find and replace using isearch, but that
> would require me going through them one at a time.  I'm wondering if
> there's a way to do this with a regex such as:
>
>     r/(=+)/-{length of n}
>
> Obviously that's a major bastardization of syntax and doesn't really
> exist in any language, but I figure this should be able to be done in
> Emacs somehow.

if your question is about interactive use, then you can use

query-replace
query-replace-regexp

after you type the find string and replace string, you can just type
“!” to replace them all in one shot in the current buffer.

If you don't want it to promp you on the first finding, you can call
replace-string instead.

If your question is about doing the replacement on whole directory of
files, you can mark the files in dired and then invoke dired-do-query-
replace-regexp.

See:

• Find and Replace with Emacs
  http://xahlee.org/emacs/emacs_find_replace.html

• Interactively Find and Replace String Patterns on Multiple Files
  http://xahlee.org/emacs/find_replace_inter.html

If your question is about writing a lisp program so that you can press
a button to do all the replacement you want without needing to answer
prompt bout find/replace string, on a single file or bunch of files,
it's very easy too. See for example:

• Elisp Lesson: Repeated Find Replace
  http://xahlee.org/emacs/elisp_repeat_replace.html

> In Christ,

• Has Religion Made Useful Contributions to Civilization?
  http://xahlee.org/p/religion_Russell.html

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Utilizing Regexp (or something else) to replace an arbitrary string length of the same character with the same string length of another character.
  2008-11-26 17:25 ` Xah Lee
@ 2008-11-27 20:24   ` Tim Visher
  2008-11-27 23:23     ` Peter Dyballa
                       ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Tim Visher @ 2008-11-27 20:24 UTC (permalink / raw
  To: Xah Lee; +Cc: help-gnu-emacs

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.

Thanks anyway.

On Wed, Nov 26, 2008 at 12:25 PM, Xah Lee <xahlee@gmail.com> wrote:
> On Nov 26, 6:41 am, "Tim Visher" <tim.vis...@gmail.com> wrote:
>> Hello Everyone,
>>
>> I have a bunch of arbitrarily long strings of '=' characters and I
>> would like to replace them one for one with '-' characters.  I know I
>> could do this with a simple find and replace using isearch, but that
>> would require me going through them one at a time.  I'm wondering if
>> there's a way to do this with a regex such as:
>>
>>     r/(=+)/-{length of n}
>>
>> Obviously that's a major bastardization of syntax and doesn't really
>> exist in any language, but I figure this should be able to be done in
>> Emacs somehow.
>
> if your question is about interactive use, then you can use
>
> query-replace
> query-replace-regexp
>
> after you type the find string and replace string, you can just type
> "!" to replace them all in one shot in the current buffer.
>
> If you don't want it to promp you on the first finding, you can call
> replace-string instead.
>
> If your question is about doing the replacement on whole directory of
> files, you can mark the files in dired and then invoke dired-do-query-
> replace-regexp.
>
> See:
>
> • Find and Replace with Emacs
>  http://xahlee.org/emacs/emacs_find_replace.html
>
> • Interactively Find and Replace String Patterns on Multiple Files
>  http://xahlee.org/emacs/find_replace_inter.html
>
> If your question is about writing a lisp program so that you can press
> a button to do all the replacement you want without needing to answer
> prompt bout find/replace string, on a single file or bunch of files,
> it's very easy too. See for example:
>
> • Elisp Lesson: Repeated Find Replace
>  http://xahlee.org/emacs/elisp_repeat_replace.html
>
>> In Christ,
>
> • Has Religion Made Useful Contributions to Civilization?
>  http://xahlee.org/p/religion_Russell.html
>
>  Xah
> ∑ http://xahlee.org/
>
> ☄
>



-- 

In Christ,

Timmy V.

http://burningones.com/
http://five.sentenc.es/ - Spend less time on e-mail

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Utilizing Regexp (or something else) to replace an arbitrary string length of the same character with the same string length of another character.
  2008-11-27 20:24   ` Tim Visher
@ 2008-11-27 23:23     ` Peter Dyballa
       [not found]     ` <mailman.1392.1227828233.26697.help-gnu-emacs@gnu.org>
  2008-11-28  1:01     ` tyler
  2 siblings, 0 replies; 7+ messages in thread
From: Peter Dyballa @ 2008-11-27 23:23 UTC (permalink / raw
  To: Tim Visher; +Cc: help-gnu-emacs Help


Am 27.11.2008 um 21:24 schrieb Tim Visher:

> The issue is that I use the characters I want to replace
> at other locations where I don't want them replaced.

You can mark a region. This will restrict substitutions to happen  
only inside the marked region.

--
Greetings

   Pete

Remember: use logout to logout.






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Utilizing Regexp (or something else) to replace an arbitrary string length of the same character with the same string length of another character.
       [not found]     ` <mailman.1392.1227828233.26697.help-gnu-emacs@gnu.org>
@ 2008-11-28  0:01       ` Andreas Politz
  0 siblings, 0 replies; 7+ messages in thread
From: Andreas Politz @ 2008-11-28  0:01 UTC (permalink / raw
  To: help-gnu-emacs

Peter Dyballa wrote:
> 
> Am 27.11.2008 um 21:24 schrieb Tim Visher:
> 
>> The issue is that I use the characters I want to replace
>> at other locations where I don't want them replaced.
> 
> You can mark a region. This will restrict substitutions to happen only 
> inside the marked region.
> 
> -- 
> Greetings
> 
>   Pete
> 
> Remember: use logout to logout.
> 
> 
> 
> 
I guess this regexp will never match

\(.\)\1*\=\1*

Maybe this will:

(defun replace-chars-around-point (new-char)
   "Replace all occurences of char at, before and after point with
NEW-CHAR."
   (interactive (list
		(read-char
		 (format "Replace %s with: "
			 (thing-at-point 'char)))))
   (let ((p (point))		    ;save-excursion does not work here
	(char (thing-at-point 'char)))
     (if (not char)
	(error "Buffer is empty"))
     (skip-chars-backward char)
     (re-search-forward (format "%s+" char))
     (replace-match (make-string (- (match-end 0)
				   (match-beginning 0)) new-char))
     (goto-char p)))


-ap


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Utilizing Regexp (or something else) to replace an arbitrary string length of the same character with the same string length of another character.
  2008-11-27 20:24   ` Tim Visher
  2008-11-27 23:23     ` Peter Dyballa
       [not found]     ` <mailman.1392.1227828233.26697.help-gnu-emacs@gnu.org>
@ 2008-11-28  1:01     ` tyler
  2 siblings, 0 replies; 7+ messages in thread
From: tyler @ 2008-11-28  1:01 UTC (permalink / raw
  To: help-gnu-emacs

"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





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-11-28  1:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-26 14:41 Utilizing Regexp (or something else) to replace an arbitrary string length of the same character with the same string length of another character Tim Visher
2008-11-26 16:31 ` Peter Dyballa
     [not found] <mailman.1299.1227715545.26697.help-gnu-emacs@gnu.org>
2008-11-26 17:25 ` Xah Lee
2008-11-27 20:24   ` Tim Visher
2008-11-27 23:23     ` Peter Dyballa
     [not found]     ` <mailman.1392.1227828233.26697.help-gnu-emacs@gnu.org>
2008-11-28  0:01       ` Andreas Politz
2008-11-28  1:01     ` tyler

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).