From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Hongyi Zhao Newsgroups: gmane.emacs.help 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 Message-ID: References: <87pmt0bdao.fsf@rub.de> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26331"; mail-complaints-to="usenet@ciao.gmane.io" Cc: help-gnu-emacs To: Stephen Berman Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Sep 23 02:24:28 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mTCXL-0006hV-9m for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 23 Sep 2021 02:24:27 +0200 Original-Received: from localhost ([::1]:37126 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mTCXK-00047l-10 for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 22 Sep 2021 20:24:26 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52208) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mTCWf-00045T-1U for help-gnu-emacs@gnu.org; Wed, 22 Sep 2021 20:23:45 -0400 Original-Received: from mail-lf1-x12b.google.com ([2a00:1450:4864:20::12b]:47073) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mTCWa-0005TU-NS for help-gnu-emacs@gnu.org; Wed, 22 Sep 2021 20:23:44 -0400 Original-Received: by mail-lf1-x12b.google.com with SMTP id z24so19582276lfu.13 for ; Wed, 22 Sep 2021 17:23:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=uErSTiRivk2dXDe3v1FHog9ez4a8GC5i0ALQt91QRq4=; b=jZg6fITSdL3P7FiUlTMrIrPFQEJsZJ6wMS/vwGkWYOeZ0OCYUDUyS5iskUO0ctZGRw eCMHezgW00Y3X42S7rJglLyIQE5cBoIFPcqs6lJhTyX0Nkd7I2KqVqi0etU34J0QGjAB ERgBsf8gIpYM8DHdkIB6bzCjgiDnRFq+TwmBYawl/o85pzmZKaHli1BeQoR6eBDQ6shJ qkrPqVVAhLFkDdE//pw+R75n6zspQg2a82KWD8yFvB06oM7H2FY9nefjr7vKrhboeV5M oK0uEiPr+XgLONG8GQEhlAkmcFn4ndD5Mj3UYPpwmxGNvPVvhxOR5gir12OfkeGyimV2 f+1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=uErSTiRivk2dXDe3v1FHog9ez4a8GC5i0ALQt91QRq4=; b=NZVq3ZRYXnRVSAPZMuvciZ5oNJKFUg0iwNN5gSkze54OQxixjDPjo9kHhaqv2M+cN4 4PxdHyfmoVPpgYqcwwauZJHHH48VhKyIBwEMwSdrSKp45yJOjh76Z/f4MhQgpcJlCT6w ztf5fCnlUYhlwXLLHDJ/XpP2b+5B64LYVGXHRd4TdKS+iYL7nBkAtDwlZuhpfprSSvdz B6CC4pYRiKYGinMY1cRPOQ85XuGMCyT7DWIMvF1KSQo2LHoZmYg6HZJ1QDuRfk9YMcTp slhMRIc8HYYL+fD2OoSALSgI9eORWO8skkp7Iu0M45C6vNLv5XGkTBgnavsilv0mNheb iTDg== X-Gm-Message-State: AOAM531jKbdTdfeOdZdDrZtWInGfMtkGO2l/gquDDPNMymPjGCzBcSPH C7XvB+JH3KYa03x9wi8vn7qokbKezuM/FIRiXCU= X-Google-Smtp-Source: ABdhPJxFHsPVNcFDIArCmkwthjjypWqzAAujJL88+iT1KQPsEfXCFfB7RTb7m4HGlG4Rbqsco+VDQ3k3MC1kM4nizg0= X-Received: by 2002:a2e:2406:: with SMTP id k6mr2207767ljk.453.1632356618734; Wed, 22 Sep 2021 17:23:38 -0700 (PDT) In-Reply-To: <87pmt0bdao.fsf@rub.de> Received-SPF: pass client-ip=2a00:1450:4864:20::12b; envelope-from=hongyi.zhao@gmail.com; helo=mail-lf1-x12b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:133157 Archived-At: On Thu, Sep 23, 2021 at 3:50 AM Stephen Berman wrote: > > On Wed, 22 Sep 2021 22:28:28 +0800 Hongyi Zhao 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