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 8177085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:25:20+00:00 2026-06-06T23:25:20+00:00

Is there an existing package that targets subexps for replacement during query-replace-regexp ? For

  • 0

Is there an existing package that targets subexps for replacement during
query-replace-regexp?

For example given the following

var foo1 = blah( properties, property_id);

var foo2 = blah(properties, property_id );

var foo3 = blah(  properties, property_id      );

I want to remove the padding around braces.

Normally, the way is to subgroup the bits you want to keep and assemble a replacement.

search:

\(var .* = blah\s-*(\)\s-*\(.*?\)\s-*\()\)

replace:

\1\2\3

However, it seems much easier to some up with a regexp that groups the bits I
want to delete than the otherway around. Like this one:

var .* = blah\s-*(\(\s-*\).*?\(\s-*\))

I’ll get two subgroups out of this. How can I target them for replacement?

EDIT: I’m asking for an interactive way to “invert” the given regexp. So the interface would be similar to query-replace-regexp

  1. enter regexp
  2. enter replacement for group 1
  3. enter replacement for group 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-06T23:25:21+00:00Added an answer on June 6, 2026 at 11:25 pm

    I think some variation of this should work:

    (defun remove-padding ()
      (interactive)
      (while (search-forward-regexp
              "var .* = [a-zA-Z_]+\\s-*(\\(\\s-*\\).*?\\(\\s-*\\))"
              nil t)
        ;; Replace the 2 subexpressions with nothing
        (replace-match "" nil t nil 2)
        (replace-match "" nil t nil 1)))
    

    However, you might also consider using a tool like indent depending on what your use cases are.

    EDIT: Below is a very minimal interactive version. The function query-replace-regexp is very complex and I have made no attempt to reproduce all of it’s functionality.

    (require 're-builder)
    (defun query-replace-subexpressions (regexp replacements)
      "REPLACEMENTS need to be in reverse order if passed from lisp!"
      ;; Read the correct number of subexpressions
      (interactive
       (let* ((re (read-from-minibuffer "Query replace subexps: "))
              (num-subexps (reb-count-subexps re))
              (replacement-list nil)
              (replacements (dotimes (rep num-subexps)
                              (setq replacement-list
                                    (cons
                                     (read-from-minibuffer
                                      (format "Replace subexpression %s with: " rep))
                                     replacement-list)))))
         (list re replacement-list)))
      ;; Search
      (let ((len (length replacements)))
        (while (search-forward-regexp regexp nil t)
          (replace-highlight (match-beginning 0) (match-end 0)
                             (point-min) (point-max) regexp
                             t case-fold-search)
          ;; Query
          (when (save-match-data (y-or-n-p "Replace this occurrence? "))
            ;; Make all the replacements
            (dotimes (i len)
              (replace-match (nth i replacements) nil nil nil (- len i)))))
        (replace-dehighlight)))
    
    
    ;; Test it out below
    (query-replace-subexpressions
     "var .* = [a-zA-Z_]+\\s-*(\\(\\s-*\\).*?\\(\\s-*\\))"
     '("" ""))
    
    var foo1 = blah(properties, property_id    );
    
    var foo2 = blah (properties, property_id );
    
    var foo3 = blah( properties, property_id      );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering if in R there is a per-existing package that can colorate
Is there existing software for discriminative reranking, such as that used by the Charniak
Are there any existing libraries in existence that will parse a datetime from a
Is there an existing tool for Java that is similar to Microsoft's CHESS ?
Is there an existing function to replace accented characters with unadorned characters in PostgreSQL?
I am looking for a language, or package in an existing language, that is
Are there existing template extract libraries in either python or php? Perl has Template::Extract
Is there any existing solution to embed the app.config file into the resulting assembly?
Are there any existing add-ons which would provide the functionality Upload image from local
Is there an existing algorithm for converting a quaternion representation of a rotation 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.