Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8978061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:31:07+00:00 2026-06-15T19:31:07+00:00

Some time ago @Oleg Pavliv explained in https://unix.stackexchange.com/questions/47615/emacs-simple-arithmetics-in-query-replace how to do simple arithmetics in

  • 0

Some time ago @Oleg Pavliv explained in https://unix.stackexchange.com/questions/47615/emacs-simple-arithmetics-in-query-replace how to do simple arithmetics in query replace (interactively) in emacs.

Now I want to use the same method for a small elisp program but it doesn’t work. Consider for example the following minimal example of elisp code:

(defun Nshift ()
(interactive)
(query-replace-regexp "\\([0-9]+\\)\\.Number" "\\,((+ 3 \\#1)).Number")
)

Now suppose I run Nshift in a buffer which contains for example the string 4.Number then I get the following error message.

match-substitute-replacement: Invalid use of `\' in replacement text

How would a correct elisp implementation of Nshift look like?

Edit:

I don’t see how Seans answer generalizes with easy and readable syntax to more complicated replacements (which I need in my application), so for example what would be the correct (and easy to read) equivalent to

(query-replace-regexp "\\([0-9]+\\)\\.Number.\\([0-9]+\\)" "\\,((+ 3 \\#1)).Number.\\,((+ 8 \\#2))")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:31:08+00:00Added an answer on June 15, 2026 at 7:31 pm

    Like this:

    (defun Nshift ()
      (interactive)
      (while (search-forward-regexp "\\([0-9]+\\)\\.Number" nil t)
        (replace-match (format "%s.Number" (+ 3 (string-to-number (match-string 1)))))))
    

    EDITED TO ADD:

    Your expanded example could be implemented in this way:

    (defun Nshift ()
      (interactive)
      (while (search-forward-regexp "\\([0-9]+\\)\\.Number\\.\\([0-9]+\\)" nil t)
        (replace-match
         (number-to-string (+ 3 (string-to-number (match-string 1))))
         nil nil nil 1)
        (replace-match
         (number-to-string (+ 8 (string-to-number (match-string 2))))
         nil nil nil 2)))
    

    It’s actually even easier than my original solution, because I forgot that replace-match has an optional fifth argument that causes it to replace just a single subexpression, and saves you from having to duplicate the fixed text (“.Number.”) in the replacement text.

    There’s some refactoring that could be done here:

    (defun increment-match-string (match-index increment)
      (replace-match
       (number-to-string (+ increment (string-to-number (match-string match-index))))
       nil nil nil match-index))
    

    Then Nshift could be implemented like so:

    (defun Nshift ()
      (interactive)
      (while (search-forward-regexp "\\([0-9]+\\)\\.Number\\.\\([0-9]+\\)" nil t)
        (increment-match-string 1 3)
        (increment-match-string 2 8)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some time ago I posted a question about what questions should a good javascript
Some time ago I read an article that explained several pitfalls of argument dependent
Some time ago i've made a simple dialog. Everything looks fine, but i'm meeting
Some time ago I developed an app for a client. It's basically a simple
Some time ago I wrote simple boids simulation using OpenCL (was school assignment), using
Some time ago I've read an article on CLR, where author showed that if
Some time ago I was asked the strange question how would I implement map
Some time ago, I wrote some code to decide which method of updating mutable
Some time ago, people here helped me very well with code. Now I'm tasked
some time ago I happend to resolve a PG related problem with this SO

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.