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

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

Re: Emacs: adding 1 to every number made of 2 digits inside a marked reg


From: Hongyi Zhao
Subject: Re: Emacs: adding 1 to every number made of 2 digits inside a marked region.
Date: Thu, 23 Sep 2021 17:00:01 +0800

On Thu, Sep 23, 2021 at 4:07 PM Stephen Berman <stephen.berman@gmx.net> wrote:
>
> On Thu, 23 Sep 2021 15:54:19 +0800 Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> > On Thu, Sep 23, 2021 at 3:24 PM Stephen Berman <stephen.berman@gmx.net> 
> > wrote:
> >>
> >> On Thu, 23 Sep 2021 08:23:26 +0800 Hongyi Zhao <hongyi.zhao@gmail.com> 
> >> wrote:
> >>
> >> > On Thu, Sep 23, 2021 at 3:50 AM Stephen Berman <stephen.berman@gmx.net> 
> >> > wrote:
> >> >>
> >> >> On Wed, 22 Sep 2021 22:28:28 +0800 Hongyi Zhao <hongyi.zhao@gmail.com> 
> >> >> wrote:
> >> >>
> >> >> > Today, I stumbled on this interesting discussion here [1]:
> >> >> >
> >> >> > --------------------
> >> >> > Imagine I've got the following in a text file opened under Emacs:
> >> >> >
> >> >> > some    34
> >> >> > word    30
> >> >> > another 38
> >> >> > thing   59
> >> >> > to      39
> >> >> > say     10
> >> >> > here    47
> >> >> >
> >> >> > and I want to turn into this, adding 1 to every number made of 2 
> >> >> > digits:
> >> >> >
> >> >> > some    35
> >> >> > word    31
> >> >> > another 39
> >> >> > thing   60
> >> >> > to      40
> >> >> > say     11
> >> >> > here    48
> >> >> > --------------------
> >> [...]
> >> > This one does nothing:
> >> >
> >> > (while (re-search-forward "\\<[0-9][0-9]\\>" nil t) (let ((x
> >> > (match-string 0))) (delete-backward-char 2) (insert (format "%d" (1+
> >> > (string-to-number x))))))
> >>
> >> It works for me.  How exactly did you use it?
> >
> > Mark set the following in scratch buffer:
> >
> > some    34
> > word    30
> >
> > M-: (while (re-search-forward "[[:digit:]]\\{2\\}" nil t) (let ((x
> >  (match-string 0))) (delete-backward-char 2) (insert (format "%d" (1+
> >  (string-to-number x)))))) RET
> >
> > Please see the screenshot of the running results on my machine in the
> > attachment.
>
> In your first screenshot it looks like point in *scratch* is after the
> number 30 when you evaluate the while-sexp.  Make sure point is before
> 34 and then it should work.

Exactly. Thank you for pointing this out. Then I do the following
testing in the scratch buffer:

some    30
word    31 *

*  This is the position of point.

M-:
(while (re-search-backward "[[:digit:]]\\{2\\}" nil t) (let ((x
 (match-string 0))) (delete-backward-char 2) (insert (format "%d" (1+
 (string-to-number x))))))

Then I obtained the following in scratch:

;; This buffer is for
tex100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231

Any hints for this strange result?

Best, HZ



reply via email to

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