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

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

Re: Utilizing Regexp (or something else) to replace an arbitrary string


From: Andreas Politz
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: Fri, 28 Nov 2008 01:01:28 +0100
User-agent: Mozilla-Thunderbird 2.0.0.17 (X11/20081018)

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


reply via email to

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