Skip to main content
Share Your Experience: Take the 2024 Developer Survey
5 votes

i have a regex that i want to apply only to the lines immediately following a \item line in a latex list. how do i do it?

How about this? I left out the slashes and other details for simplicity, but it still demonstrates a solution: \(item.*\n.+\)Property Agreement -> \1{Property Agreement} Note that if you do this ...
Tom's user avatar
  • 1,260
4 votes

How to replace (blah) with {blah} for a specific string

You can do this with query-replace-regexp (bound to C-M-% by default), which prompts for a regexp and a replacement, then asks for confirmation at each match (useful for preventing accidents). The ...
db48x's user avatar
  • 18.3k
4 votes
Accepted

What is the replace regexp string for applying 3 digit grouping to any number?

I am not sure about your use-case, but anyway I would recommend using any of the methods mentioned in this reddit post. So, if you'd like to 'commify' all numbers in some buffer, then you could use ...
dalanicolai's user avatar
  • 7,855
3 votes

How can I interactively replace text in all files in all subfolders?

18.20 Filesets: If you regularly edit a certain group of files, you can define them as a fileset. This lets you perform certain operations, such as visiting, query-replace, and shell commands on all ...
shynur's user avatar
  • 5,318
3 votes

Automatically save files after multi-file search/replace

You could trigger save-buffer after the replace. I believe the following should work: (add-hook 'replace-update-post-hook 'save-buffer)
aadcg's user avatar
  • 1,248
3 votes

Using a variable as the REGEXP in `query-replace-regexp`

M-: (query-replace-regexp foo "bar") RET
gigiair's user avatar
  • 2,186
3 votes

How to duplicate text but increment a numeric part of a name?

query-replace-regexp can do the trick. Select the region you want for replacing and : C-M % \brect1\b RET rect\,(1+ \#) RET ! for more information (info "(emacs)Regexp Replacement") ;;;;;...
gigiair's user avatar
  • 2,186
3 votes
Accepted

Function to replace strings from region and save result in kill-ring

One implementation might be this: (defun changestring (&optional arg) "Replace some character in string and save to kill-ring." (interactive "p") ;; conditionally let ...
Maxim Kim's user avatar
  • 1,766
2 votes
Accepted

How can I get the search string from incremental regexp search to use with replace regexp

Is this for interactive use? I suppose so. As the doc string for replace-regexp says: This function is for interactive use only; in Lisp code use `re-search-forward' and `replace-match' ...
Drew's user avatar
  • 77.9k
2 votes
Accepted

Equivalent of query-replace-regexp for multiple regexps

Try this. Enter an empty regexp interactively to stop adding regexp/replacement pairs, and start replacements. (defun query-multi-replace-regexp (&rest pairs) "Query replace for each ...
phils's user avatar
  • 51.5k
2 votes

How to find a newline with a regular expression in query-replace-regexp?

OK, this has been driving me nuts every time I've run into it for over a year now and I finally sat down and figured out what is going on... Unfortunately I don't have a fix---only the actual ...
kspurgin's user avatar
2 votes
Accepted

regexp fails over expression involving {}

In fact, the problem comes from the unescaped [ and ]. M-x query-replace-regexp \\includepdf\[pages={1-}\]{.*} → \\includepdf[pages={1-}]{TEST} works for me.
Fran Burstall's user avatar
2 votes
Accepted

evil: Make `%s/x/y/gc` use case insensitive matching but do case sensitive replaces

Looking at the code of evil-ex-substitute (using C-h f), the value for case-replace is set to the value of case-fold-search in the let* form. So it looks like there is no configuration option provided ...
dalanicolai's user avatar
  • 7,855
2 votes

How do you use a variable in the "replacement" part of query-replace-regexp?

(Might as well make my comment an answer, I guess. I can delete it if a reply indicates that it's not helpful.) I don't understand the question. If you're always using regexp-quote on the first ...
Drew's user avatar
  • 77.9k
2 votes
Accepted

isearch-forward-regex does not find a regex found by re-builder

Your question is unclear. If I copy this exact same string and invoke isearch-forward-regexp Emacs does not find the dates. What do you mean by that? Isearch doesn't automatically search for the ...
phils's user avatar
  • 51.5k
2 votes
Accepted

Is it possible for a query-replace-regexp to replace the value with a computed value?

The answer is Yes. C-h f query-replace-regexp says: In interactive calls, the replacement text can contain ‘\,’ followed by a Lisp expression. Each replacement evaluates that expression to compute ...
NickD's user avatar
  • 30.5k
2 votes

Is it possible for a query-replace-regexp to replace the value with a computed value?

Here is the solution for my .zsh_history example: The search string: \(: \)\([0-9]\{10\}\)\(:0;\) The replacement string: \,(concat (format "%6d " (line-number-at-pos)) (rgx-get-time-string ...
vfclists's user avatar
  • 1,423
2 votes

Am I using "replace-regexp-in-string" the right way?

When in doubt, ask Emacs. Use C-h f to look up the help for the function replace-regexp-in-string. You’ll get something like this: replace-regexp-in-string is a byte-compiled function defined in subr....
db48x's user avatar
  • 18.3k
1 vote
Accepted

Efficient way to change the format of a date in orgmode

One way to do what you want: M-: (replace-regexp "\\([0-9]\\{4\\}\\)/\\([0-9]\\{2\\}\\)/\\([0-9]\\{2\\}\\)" "<\\1-\\2-\\3>" nil (point-min) (point-max)) This looks more ...
NickD's user avatar
  • 30.5k
1 vote
Accepted

How do you use a variable in the "replacement" part of query-replace-regexp?

In your case, since you're replacing a fixed string by a fixed string, use query-replace instead of query-replace-regexp. In general, to replace a regexp by a fixed string, use replace-quote (missing ...
Gilles 'SO- stop being evil''s user avatar
1 vote
Accepted

Highlight query-replace-regexp results while searching

EDIT I just noticed that my answer is valid for Emacs 29.0.50, but not for Emacs 28.1. Unfortunately, I can not easily find when this was added. END EDIT This is default behavior when using M-x ...
dalanicolai's user avatar
  • 7,855
1 vote
Accepted

Using query-replace-regex to partially replace

Assuming the trailing arbitrary text can legitimately contain commas, so you can't safely replace every , with \, throughout: You don't need to escape the #. Based on your description, you don't need ...
Phil Hudson's user avatar
  • 1,741
1 vote
Accepted

Is it possible to mimic sed's "change line" feature?

Is it possible to… The answer to this type of question is always yes. In your case, just run (replace-regexp "^foo.*$" "bar"). You can do this interactively with query-replace-...
db48x's user avatar
  • 18.3k
1 vote
Accepted

Can replace-regexp-lax-whitespace manage "~" as a whitespace?

I found a solution. I need to redefine (let-binding) both replace-regexp-lax-whitespace and search-whitespace-regexp: (defvar biblio-publishers-info-list '(("Cambridge University Press" . &...
Gabriele's user avatar
  • 1,618
1 vote
Accepted

How to match two consecutive identical words except for the case of the initial characters?

There are numerous bugs in that code, but perhaps something like this is what you want? (let ((case-fold-search nil) (word "\\b\\([[:word:]]\\)\\([[:word:]]*\\)\\b[[:space:]]*") ...
phils's user avatar
  • 51.5k
1 vote

Find and recursively delete (with query) consecutive duplicate words not necessarily in the same line and/or same case

You can use query-replace-regexp with the following parameters: \b\(\w+\)\W+\1 for matching a word (saved as a matching subexpression by means of \( and \)), followed by non-word characters, then ...
Juancho's user avatar
  • 5,465
1 vote

Matching non-# in Regexp

Okay, I figured it out. I was misusing the word boundary markers (which I don't understand how so a comment or two would be great.) This is the code I want to use ^\([A-z0-9_.]+\) \([^# <C-q 012&...
mikemtnbikes's user avatar
1 vote

Search and Replace for a block on many lines in a dozen files

It is generally easier to match smaller, specific patterns, and then replace the region between them (although in this case the regexp as suggested by NickD works fine (except for a possible extra ...
dalanicolai's user avatar
  • 7,855
1 vote
Accepted

Using a variable as the REGEXP in `query-replace-regexp`

In reading the doc of query-replace-regexp, it mentions the \, syntax in the replacement text part, but not in the regex part: In interactive calls, the replacement text can contain ‘,’ followed by a ...
NickD's user avatar
  • 30.5k
1 vote

How to find a newline with a regular expression in query-replace-regexp?

First, note that query-replace-regexp matches either from the point to the end of the buffer or in the active region. So if your point is after what you are trying to match it will not work. With that ...
Tephra's user avatar
  • 831

Only top scored, non community-wiki answers of a minimum length are eligible