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

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

Re: Regex replace for numbers


From: Nicolas Richard
Subject: Re: Regex replace for numbers
Date: Fri, 03 Oct 2014 07:25:08 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.93 (gnu/linux)

Robert Thorpe <rt@robertthorpeconsulting.com> writes:

> I'm not very good with Emacs regex, I need to search-and-replace on some 
> numbers.
>
> How can I detect unnecessary zeros at the end of a number and chop them
> off?  E.g. turn 567.45000 to 567.45 without also turning 6700 to 67.

Instead of solving it with a regexp, I suggest some lisp :

(save-excursion
  (goto-char (point-min))
  (while (re-search-forward "[0-9]" nil t )
    (let ((bounds (bounds-of-thing-at-point 'sexp))
          (number (number-at-point)))
      (when number
        (delete-region (car bounds) (cdr bounds))
        (insert (format "%s" number))))))

(This might not work in modes other than emacs-lisp mode, due to how
number-at-point works.)

you can test it on :

foo0
5054.210
567.45000
567.04500
127.0.1.0
500
005

and get:

foo0
5054.21
567.45
567.045
127.0.1.0
500
5

-- 
Nicolas Richard



reply via email to

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