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

  • Home
  • SEARCH
  • 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 7560661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:57:39+00:00 2026-05-30T12:57:39+00:00

Here’s a Find-Replace which swaps values: Find: right\|left Repl: \,(if (equal right \&) left

  • 0

Here’s a Find-Replace which swaps values:

Find: right\|left
Repl: \,(if (equal "right" \&) "left" "right")

Here’s an attempt to convert it into interactive function:

(defun swaps (rit lft)
  "Swaps rit to lft."
  (interactive "sChange this: 
sTo this: ")
  (save-excursion
    (goto-char (region-beginning))
    (while (search-forward-regexp ("%s\\|%s" rit lft) nil t) 
      (replace-match (if (equal rit \\&) lft rit) t nil))))

I also tried rit\\|lft and rit\|lft instead of ("%s\\|%s" rit lft)…

Edit:

The answer is:

(defun swaps (rit lft)
  "Swaps rit to lft."
  (interactive "sChange this: 
sTo this: ")
  (save-excursion
    (goto-char (region-beginning))
    (while  (search-forward-regexp (format "%s\\|%s" 
                                          (regexp-quote rit)
                                          (regexp-quote lft)) (region-end) t) 
      (replace-match (if (equal rit (match-string 0)) lft rit) t nil))))
  • 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-05-30T12:57:40+00:00Added an answer on May 30, 2026 at 12:57 pm

    ("%s\\|%s" rit lft) isn’t a valid Lisp expression: when it’s evaluated Emacs will complain that "%s\\|%s" isn’t a function. You probably want to do

     (format "%s\\|%s" rit lft)
    

    and it would be better to use regexp-quote in case your strings contain regexp special characters:

    (format "%s\\|%s" 
            (regexp-quote rit)
            (regexp-quote lft))
    

    Alternatively, you could also use the regexp-opt function, which constructs an efficient regexp to match any one of a list of strings:

    (regexp-opt (list rit lft))
    

    \\& only represents the matched string within a replacement argument to replace-regexp, replace-match and similar functions. In other Lisp code you need to use (match-string 0).

    Finally, if you only want this to work on the region you should probably supply (region-end) as the second argument to search-forward-regexp.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),
Here's a problem I ran into recently. I have attributes strings of the form
Here is my code, which takes two version identifiers in the form 1, 5,
Here a simple question : What do you think of code which use try
Here is another spoj problem that asks how to find the number of distinct
Here is binding: <Image Width=16 Height=16 Source={Binding SwitchForImage, Converter={StaticResource stringToImage}} HorizontalAlignment=Left> </Image> and here
Here is an example: I have a file 1.js, which has some functions. I
Here is my situation: I have a controller action called OrderFromCategory which fills a
Here is a function from Dreamweaver, if (($strUsers == ) && false) what the
Here is a section of code which is giving me a different answer to

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.