unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Emacs: adding 1 to every number made of 2 digits inside a marked region.
@ 2021-09-22 14:28 Hongyi Zhao
  2021-09-22 19:50 ` Stephen Berman
                   ` (5 more replies)
  0 siblings, 6 replies; 54+ messages in thread
From: Hongyi Zhao @ 2021-09-22 14:28 UTC (permalink / raw)
  To: help-gnu-emacs

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?

Regards
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province



^ permalink raw reply	[flat|nested] 54+ messages in thread

end of thread, other threads:[~2021-09-28  1:51 UTC | newest]

Thread overview: 54+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-09-22 14:28 Emacs: adding 1 to every number made of 2 digits inside a marked region Hongyi Zhao
2021-09-22 19:50 ` Stephen Berman
2021-09-22 21:04   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-23  0:23   ` Hongyi Zhao
2021-09-23  7:24     ` Stephen Berman
2021-09-23  7:54       ` Hongyi Zhao
2021-09-23  8:07         ` Stephen Berman
2021-09-23  9:00           ` Hongyi Zhao
2021-09-23 10:07             ` Stephen Berman
2021-09-23 10:13               ` Hongyi Zhao
2021-09-23 10:23                 ` Stephen Berman
2021-09-23 12:58                   ` Hongyi Zhao
2021-09-23 13:29                     ` Stephen Berman
2021-09-23 14:25                       ` Hongyi Zhao
2021-09-22 20:38 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-23  0:42   ` Hongyi Zhao
2021-09-23  0:46     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-23  0:56       ` Hongyi Zhao
2021-09-23  2:06         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-23  2:12           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-23  0:57     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-23  8:02   ` Hongyi Zhao
2021-09-23  8:37 ` Andreas Röhler
2021-09-23  9:05   ` Hongyi Zhao
2021-09-23  9:58   ` Hongyi Zhao
2021-09-23 10:10     ` Gregory Heytings
2021-09-23 10:17       ` Hongyi Zhao
2021-09-23 13:08         ` Gregory Heytings
2021-09-23 11:59     ` Andreas Röhler
2021-09-23 13:14       ` Hongyi Zhao
2021-09-23 13:51         ` Andreas Röhler
2021-09-23 14:23           ` Hongyi Zhao
2021-09-23  9:26 ` Gregory Heytings
2021-09-23  9:53   ` Hongyi Zhao
2021-09-23 10:02     ` Gregory Heytings
2021-09-23 14:13     ` [External] : " Drew Adams
2021-09-23 23:50       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-24  0:23         ` Drew Adams
2021-09-24  2:34           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-24  3:01             ` Drew Adams
2021-09-24  7:25         ` Gregory Heytings
2021-09-24  7:32           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-23 11:50 ` Andreas Röhler
2021-09-26 22:54 ` Michael Heerdegen
2021-09-26 23:50   ` Arthur Miller
2021-09-27  1:02   ` Arthur Miller
2021-09-27  1:22     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-27  5:57       ` Hongyi Zhao
2021-09-28  1:32         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-27 10:59       ` Arthur Miller
2021-09-28  1:34         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28  1:51           ` Arthur Miller
2021-09-27  1:03   ` Arthur Miller
2021-09-27 14:08     ` Arthur Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).