[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 08:23:26 +0800 |
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
> > --------------------
> >
> > I tried all the ELISP codes suggested there, and only found that the
> > following one is valid:
> >
> > C-M-% \b[0-9][0-9]\b return \,(1+ \#&)
> >
> > The other two can't do the trick:
> >
> > First one:
> >
> > (defun add-1-to-2-digits (b e)
> > "add 1 to every 2 digit number in the region"
> > (interactive "r")
> > (goto-char b)
> > (while (re-search-forward "\\b[0-9][0-9]\\b" e t)
> > (replace-match (number-to-string (+ 1 (string-to-int (match-string
> > 0)))))))
> >
> > Validating method:
> >
> > `M-:' input-the-above-code-here, RET, `M-x add-1-to-2-digits'.
> >
> > And the second:
> >
> > (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-int x))))))
> >
> > Validating method:
> >
> > `M-:' input-the-above-code-here, RET
> >
> > [1]
> > https://stackoverflow.com/questions/2686593/emacs-adding-1-to-every-number-made-of-2-digits-inside-a-marked-region
> >
> > Any hints/comments/enhancements for these methods will be greatly
> > appreciated?
>
> `string-to-int' was made obsolete long ago and removed in Emacs 26 (see
> NEWS.26); replace it with `string-to-number' in those code snippets and
> they'll work.
This one works:
(defun add-1-to-2-digits (b e)
"add 1 to every 2 digit number in the region"
(interactive "r")
(goto-char b)
(while (re-search-forward "\\b[0-9][0-9]\\b" e t)
(replace-match (number-to-string (+ 1 (string-to-number
(match-string 0)))))))
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))))))
HZ
- Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/22
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Stephen Berman, 2021/09/22
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Emanuel Berg, 2021/09/22
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region.,
Hongyi Zhao <=
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Stephen Berman, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Stephen Berman, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Stephen Berman, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Stephen Berman, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Stephen Berman, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/23