* Regex replace for numbers
@ 2014-10-03 0:33 Robert Thorpe
2014-10-03 1:07 ` Marcin Borkowski
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Robert Thorpe @ 2014-10-03 0:33 UTC (permalink / raw)
To: help-gnu-emacs
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.
BR,
Robert Thorpe
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Regex replace for numbers
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 5:25 ` Nicolas Richard
[not found] ` <mailman.10347.1412313926.1147.help-gnu-emacs@gnu.org>
2 siblings, 1 reply; 8+ messages in thread
From: Marcin Borkowski @ 2014-10-03 1:07 UTC (permalink / raw)
To: help-gnu-emacs
On 2014-10-03, at 02:33, Robert Thorpe wrote:
> 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.
What about
M-%
\([0-9]+\)\.\([0-9]*?\)0+ RET
\1.\2 RET
?
> BR,
> Robert Thorpe
Hth,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Regex replace for numbers
2014-10-03 1:07 ` Marcin Borkowski
@ 2014-10-03 1:41 ` Robert Thorpe
2014-10-03 2:45 ` Yuri Khan
0 siblings, 1 reply; 8+ messages in thread
From: Robert Thorpe @ 2014-10-03 1:41 UTC (permalink / raw)
To: Marcin Borkowski; +Cc: help-gnu-emacs
Marcin Borkowski <mbork@wmi.amu.edu.pl> writes:
> On 2014-10-03, at 02:33, Robert Thorpe wrote:
>
>> 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.
>
> What about
>
> M-%
> \([0-9]+\)\.\([0-9]*?\)0+ RET
> \1.\2 RET
That needs C-M-%, regexp-replace. If I have a number like 456.0040 then
it replaces the first two zeros giving 456.40, which is wrong. It seems
that shortest-match regexps aren't very useful here. If I remove the ?
in the regexp then it works, but it only removes one layer of zeros at a
time, so e.g. 567.45000 becomes 567.4500.
BR,
Robert Thorpe
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Regex replace for numbers
2014-10-03 1:41 ` Robert Thorpe
@ 2014-10-03 2:45 ` Yuri Khan
0 siblings, 0 replies; 8+ messages in thread
From: Yuri Khan @ 2014-10-03 2:45 UTC (permalink / raw)
To: Robert Thorpe; +Cc: help-gnu-emacs@gnu.org, Marcin Borkowski
On Fri, Oct 3, 2014 at 8:41 AM, Robert Thorpe
<rt@robertthorpeconsulting.com> wrote:
>> \([0-9]+\)\.\([0-9]*?\)0+ RET
>> \1.\2 RET
>
> That needs C-M-%, regexp-replace. If I have a number like 456.0040 then
> it replaces the first two zeros giving 456.40, which is wrong. It seems
> that shortest-match regexps aren't very useful here. If I remove the ?
> in the regexp then it works, but it only removes one layer of zeros at a
> time, so e.g. 567.45000 becomes 567.4500.
So what you need to match is a string of digits, then a decimal point,
then a non-greedy string of digits, then a (possibly empty but greedy)
string of zeros, and finally an end of word.
\([0-9]+\.[0-9]*?\)0+\> replace with \1
Note that this won’t touch instances which have a letter immediately after it:
1234.5678000s
If you want that too, try this:
\([0-9]+\.[0-9]*?\)0+\([^0-9]|\') replace with \1\2
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Regex replace for numbers
2014-10-03 0:33 Regex replace for numbers Robert Thorpe
2014-10-03 1:07 ` Marcin Borkowski
@ 2014-10-03 5:25 ` Nicolas Richard
[not found] ` <mailman.10347.1412313926.1147.help-gnu-emacs@gnu.org>
2 siblings, 0 replies; 8+ messages in thread
From: Nicolas Richard @ 2014-10-03 5:25 UTC (permalink / raw)
To: Robert Thorpe; +Cc: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 8+ messages in thread
[parent not found: <mailman.10347.1412313926.1147.help-gnu-emacs@gnu.org>]
* Re: Regex replace for numbers
[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
0 siblings, 1 reply; 8+ messages in thread
From: Udyant Wig @ 2014-10-03 11:43 UTC (permalink / raw)
To: help-gnu-emacs
Nicolas Richard <theonewiththeevillook@yahoo.fr> writes:
| 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.)
Is there a way to save the major-mode of the buffer before entering the
above code, change to emacs-lisp-mode and then to enable the original
major-mode again?
| <snip tests>
--
Udyant Wig
GitHub: https://github.com/udyant
Poetry: http://www.writing.com/main/profile/biography/frosthrone
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Regex replace for numbers
2014-10-03 11:43 ` Udyant Wig
@ 2014-10-03 14:40 ` Nicolas Richard
2014-10-03 19:22 ` Robert Thorpe
0 siblings, 1 reply; 8+ messages in thread
From: Nicolas Richard @ 2014-10-03 14:40 UTC (permalink / raw)
To: Udyant Wig; +Cc: help-gnu-emacs
Udyant Wig <udyantw@gmail.com> writes:
> Is there a way to save the major-mode of the buffer before entering the
> above code, change to emacs-lisp-mode and then to enable the original
> major-mode again?
You don't really need to do that. Simply using the syntax table should
be sufficient in this case :
(with-syntax-table emacs-lisp-mode-syntax-table
(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)))))))
--
Nicolas Richard
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Regex replace for numbers
2014-10-03 14:40 ` Nicolas Richard
@ 2014-10-03 19:22 ` Robert Thorpe
0 siblings, 0 replies; 8+ messages in thread
From: Robert Thorpe @ 2014-10-03 19:22 UTC (permalink / raw)
To: Nicolas Richard; +Cc: help-gnu-emacs, udyantw
Nicolas Richard <theonewiththeevillook@yahoo.fr> writes:
> Udyant Wig <udyantw@gmail.com> writes:
>> Is there a way to save the major-mode of the buffer before entering the
>> above code, change to emacs-lisp-mode and then to enable the original
>> major-mode again?
>
> You don't really need to do that. Simply using the syntax table should
> be sufficient in this case :
>
> (with-syntax-table emacs-lisp-mode-syntax-table
> (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)))))))
Thanks everyone. I'll try the elisp solution and the regexps solution.
BR,
Robert Thorpe
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2014-10-03 19:22 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
[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
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.