all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Nicolas Richard <theonewiththeevillook@yahoo.fr>
To: Robert Thorpe <rt@robertthorpeconsulting.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Regex replace for numbers
Date: Fri, 03 Oct 2014 07:25:08 +0200	[thread overview]
Message-ID: <874mvliv2j.fsf@geodiff-mac3.ulb.ac.be> (raw)
In-Reply-To: <87zjdexa89.fsf@robertthorpeconsulting.com> (Robert Thorpe's message of "Fri, 03 Oct 2014 01:33:58 +0100")

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



  parent reply	other threads:[~2014-10-03  5:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-03  0:33 Regex replace for numbers Robert Thorpe
2014-10-03  1:07 ` Marcin Borkowski
2014-10-03  1:41   ` Robert Thorpe
2014-10-03  2:45     ` Yuri Khan
2014-10-03  5:25 ` Nicolas Richard [this message]
     [not found] ` <mailman.10347.1412313926.1147.help-gnu-emacs@gnu.org>
2014-10-03 11:43   ` Udyant Wig
2014-10-03 14:40     ` Nicolas Richard
2014-10-03 19:22       ` Robert Thorpe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874mvliv2j.fsf@geodiff-mac3.ulb.ac.be \
    --to=theonewiththeevillook@yahoo.fr \
    --cc=help-gnu-emacs@gnu.org \
    --cc=rt@robertthorpeconsulting.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.