[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Regex replace for numbers
From: |
Yuri Khan |
Subject: |
Re: Regex replace for numbers |
Date: |
Fri, 3 Oct 2014 09:45:50 +0700 |
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