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 ...
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 ...
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 ...
4
votes
Accepted
How to replace any letter with the next in the alphabet?
You can certainly do it with the interactive regexp replacement, and you're correct in understanding that characters are numbers, but you have to realise that you're dealing with strings rather than ...
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 ...
3
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 ...
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)
3
votes
Using a variable as the REGEXP in `query-replace-regexp`
M-: (query-replace-regexp foo "bar") RET
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")
;;;;;...
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 ...
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' ...
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 ...
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 ...
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.
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 ...
2
votes
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 ...
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 ...
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 ...
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 ...
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....
2
votes
How to call query-replace-regexp in elisp with capture groups and functions in replacement string
Always check the documentation. C-h f will show you the documentation for any named function, and if you read the documentation for query-replace-regexp you will find the following line:
In ...
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 ...
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 ...
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 ...
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-...
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" . &...
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:]]*")
...
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 ...
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&...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
query-replace-regexp × 43regular-expressions × 22
replace × 8
query-replace × 6
search × 4
dired × 2
isearch × 2
words × 2
org-mode × 1
org-export × 1
latex × 1
buffers × 1
evil × 1
org-table × 1
files × 1
variables × 1
text-editing × 1
html × 1
highlighting × 1
whitespace × 1
keyboard-macros × 1
syntax-table × 1
error-handling × 1
newlines × 1
occur × 1