All Questions
Tagged with replace regular-expressions
64 questions
0
votes
1
answer
30
views
Paragraphs in regexps that are not defined in the minibuffer
I sometimes have epubs that I prefer reading in org-mode, so I convert them into text files and format them while reading. That leaves all the footnotes as bare numbers, so I used replace-regexp to ...
0
votes
1
answer
57
views
Replacing in nested regexps
I'm having to replace the configurations of keymaps in my use-package with define-key expressions and thought I'd use that to get more proficient with using regexp-replace. The situation is this:
(:...
0
votes
0
answers
45
views
Regexp searches that repeatedly cover expressions
I'm just going through an old Latex-text I've written and wanted to use regexp-replace to make any pair of brackets containing at least one other pair of brackets into a \left(.*\right)-expression. ...
1
vote
1
answer
215
views
replace nth-match of a regexp in a string
I'm looking for a function that can replace the nth-match in a string.
Here's an example. 2 is the index of the nth-match, [0-9]+ is the regular expression to search. foo is the replacement and "...
1
vote
1
answer
42
views
function needed to replace short, static list of keywords
In my workflow I copy emails into an orgmode file. I'd like to have head of the email formatted. In english emails (just as an explanation) I'd like to have *To:* instead of To:, because orgmode then ...
0
votes
1
answer
65
views
Multiple regexp replace in string
I need to search for every declared variable in a string with format %^{var}, prompt a value and replace in place.
(let ((primitive "(%^{size}, %^{center})"))
(if (string-match "%^{\\(...
0
votes
2
answers
414
views
Regex to find and replace a specific character inside of multiple quoted strings in a string
Given an example string of JSON (formatted here for reading, actual input may or may not be formatted):
{
"page":1,
"per_page":6,
"total":12,
"...
1
vote
1
answer
16
views
Switch a TeX \macro and a script on a region of text
How can I search and replace in a region of a (TeX)buffer all occurences of \macro{foo}_{bar} to \macro{foo_{bar}}, foo and bar not being "constants" ?
0
votes
2
answers
48
views
Replacing words using multiple rules
This function is supposed to replace matches with a shortened version using multiple rules rather than just a single one. For the word counter, coun gets replaced by k, resulting in the shortened ...
0
votes
1
answer
61
views
Is it possible to mimic sed's "change line" feature?
I'd like to write an elisp function that allows me to replace any line in my buffer matching a regex pattern <PATTERN> with text <TEXT>. In other words, if line N matches <PATTERN>, ...
1
vote
1
answer
126
views
How to I replace several texts like (1), (2), (3) etc. with fixed text (1), fixed text (2), fixed text (3) in one go?
My text contains several occurrences like "(1)", "(2)", "(3)". I want to replace these with the general format like "fixed text (1)", "fixed text (2)",...
0
votes
1
answer
54
views
Regexp replacement features
In the replacement part of :s in evil I can add commands like \l to lowercase the next char and \L to lowercase everything that follows, but I am unable to find documentation for that anywhere.
I ...
2
votes
2
answers
129
views
How can I fix my re-replace-region function in which match data gets clobbered?
I have an ancient function I've been using for decades which performs a regexp replacement in a region:
(defun re-replace-region (begin end old new)
"Replace occurrences of REGEXP with TO-STRING ...
2
votes
1
answer
120
views
How to replace the first element of the `kill-ring` by a modification of it?
I am trying to modify the string that is to yanked
My goal is to modify certains paths before yanking, in order to transform them in relative to path actual buffer file.
Following this post, I tried ...
0
votes
1
answer
502
views
replace-regexp-in-string: How to prepend a string to the entire string matched?
I am adding divider lines to certain sections of the reports generated by the binary ledger [ https://www.ledger-cli.org/ ] through the function ledger-do-report in the ledger-mode Lisp library [ ...
1
vote
1
answer
100
views
How can I get the search string from incremental regexp search to use with replace regexp
If I want to do a replace-regexp I usually start with a isearch-forward-regexp so I can get immediate visual feedback on the search string.
From there I'd like to grab the search string for using in ...
0
votes
1
answer
506
views
Regex to replace subset of repeated pattern
ABC、DEF, GHI, JKL、MNO
PQR、STU, VWX、YZ
How do I replace all , s with 、 in the repeated pattern above?
The repeated pattern can be mapped with the regex below:
"^\\([A-Z]+、\\)\\(\\([A-Z]+\\), \\)+&...
1
vote
1
answer
103
views
Decode double-back-slashes-prefixed octal codes in 'utf-8
I have a string "\\303\\273" that I want to decode into û.
It's known that (decode-coding-string "\303\273" 'utf-8) evals to û.
However, I'm not able to translate "\\303\\273&...
2
votes
2
answers
584
views
Remove multiple blank lines in a buffer, leave one
I'd like to take a buffer which looks like this:
one
two
three
four
five
And turn it into this
one
two
three
four
five
I've tried using delete-blank-line, however, it will only effect the area ...
0
votes
1
answer
165
views
Programmatically find-and-replace custom sequence
I need to write a command which will apply to my buffer custom, predefined copy-and-replace sequence.
What I have is:
(defun custom-processor ()
"Formats logs to human-readable format"
((replace-...
0
votes
1
answer
79
views
How to set a variable that matches different type of strings?
I have these strings:
First Type:
arXiv:1234.56789
arXiv:1234.56789v1
Second type:
hep-th/123456789
hep-th.AB/12345689
hep-th/123456789v1
hep-th.AB/12345689
Belong to the second type even strings ...
2
votes
1
answer
69
views
Query replace regexp with a distant match?
I have a single transcript file for a set of course lectures I am watching. After doing some formatting on the file. I have it broken up into sets of lessons where the main heading is the lesson title ...
0
votes
1
answer
2k
views
Replace full-width numerals with half-width numerals
I'd like to convert numerals from half-width to full-width characters with a simple regex search and replace as follows:
replace-regexp-in-string regexp rep string &optional fixedcase literal ...
1
vote
1
answer
293
views
Regex: Don't Match Text Between Curly Braces (LaTeX Commands)
I've been using pandoc and emacs to convert beamer presentations to markdown format. In the beamer presentations we highlighted certain code in example code blocks red. Pandoc has left me to get rid ...
1
vote
1
answer
445
views
How to paste a multiline regex from re-builder into `replace-regexp`
I built with M-x re-builder a regex to match empty quoted lines in emails. In read syntax it is:
"^>\\s-*\n>"
How can I copy this regex in re-builder, so that I can paste it in the first ...
8
votes
2
answers
4k
views
Emacs replace-regexp reference the match in the replace argument
Is there an easy way to use replace-regexp to do something like:
XYZ001
XYZ002
XYZ003
to
'XYZ001'
'XYZ002'
'XYZ003'
i.e. replace-regexp XYZ* 'XYZ*'
Is replace-regexp the the wrong tool for the ...
1
vote
1
answer
460
views
Elisp expressions in projectile-replace-regexp
I need to execute a regular expression with case conversion in a entire project. If I run query-replace-regexp in a single file informing '\([A-Z]+\)' and '\,(downcase \1)', it works as expected. But ...
1
vote
1
answer
2k
views
search and replace double space
I've only recently started using emacs, for professional reasons.
I'm trying to replace all double spaces in a text file by single ones using regex.
In C-M-s the expression \s-\{2\} matches two spaces ...
6
votes
3
answers
1k
views
Regexp replace to match a string, but not match a superstring
Let's say I want to replace the string "Vector" by "VectorBase", but there are existing instances of "VectorBase". So I would like to omit "VectorBase". What is an elegant way to achieve this?
An ...
0
votes
1
answer
96
views
How remove text before "," (include) in every lines?
suppose I has the next text:
-1,0x0 Fireplace HD
1160,0x0 Music HD
719,1 HD
-1,1 MUSIC CHANNEL
154,1+1
-1,100% News
-1,100AutoMoto TV
I need to remove text before "," (include ,)
So the result must ...
1
vote
3
answers
289
views
Function that does `M-x replace-regexp` or `M-x occur` with a fixed pattern and replacement strings
I often use M-x replace-regexp and M-x occur with some fixed regexp pattern and replacement string on many files.
Doing it manually by opening each file in an Emacs buffer and hitting the keys is ...
1
vote
1
answer
246
views
Advanced search and replace
I have a file, with a lot of instances of
...
fclose(f1);
fclose(f2);
fclose(f3);
...
I'd like to replace these with
...
if(fclose(f1)!=0) return 1;
if(fclose(f2)!=0) return 1;
if(fclose(f3)!=0) ...
0
votes
2
answers
127
views
Create strings of a wordlist using replace-regexp
I have a word list with a lot of words that I want to convert into a json file. To do this I need to make them strings and add a comma in the end of all words. I figured it would be nice to use ...
0
votes
1
answer
139
views
Using a predefined Regex on a yank
can you please help me to write a small emacs function? This is the first lisp function I am writing and I think I am getting confused with point and mark placement.
I want to write a function that ...
1
vote
1
answer
494
views
Produce \' in a string replacement
As you know, functions such as replace-regexp-in-string and replace-match have an argument LITERAL, meaning ...
If optional ... arg LITERAL is non-nil, insert NEWTEXT literally.
Otherwise treat ‘\’ ...
1
vote
3
answers
2k
views
Help with `replace-regexp-in-string`, to replace only first instance in every line
I have the following string:
// const asdf = 'asdf'
// // comment
// const asdf2 = asdf
And I want to run a regexp replace to make it look as follows:
const asdf = 'asdf'
// comment
const asdf2 = ...
17
votes
2
answers
3k
views
Incrementally replace a given string
Let us say I have a text like so below:
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
Now I want to replace the nn with numbers like so
AC(0)
AC(1)
AC(2)
AC(3)
AC(4)...
3
votes
1
answer
466
views
Find/replace in differrent file extensions
Windows 10, Emacs 25.1, Dired+, xah-find
I use package xah-find for find/replace text in many files.
OK. It's work.
If I want to find in all java files I use the next regexp:
File path regex: \.java\...
1
vote
1
answer
425
views
Executing multiple replacement regexps against a string
I wanted to write a function that "auto-corrects" a string, based on a list of regular expressions and their respective replacements.
I've come up with this, and it works:
(setq auto-corrections-...
0
votes
1
answer
341
views
Using result of an Elisp function as input to replace-regexp in interactive mode
I am using replace-regexp interactively. Can I programmatically refer to a match or otherwise interpolate values in the replacement string? In Ruby, for example, it is easily done, by using the #{ <...
1
vote
1
answer
249
views
elisp replace float number with half value
I need to replace below string:
(24.0000,28.0000) .. controls (27.3100,28.0000) and (29.9800,25.3100) .. (29.9800,22.0000)
to:
(12,14) .. controls (13.655,14) and (14.99,12.655) .. (14.99,11)
The ...
3
votes
2
answers
338
views
Query-replace-regexp alternative capable of handling nested braces?
TL;DR
Is there some method in Emacs to replace text with nested braces/parentheses?
Example
I had a situation where I had to replace
\dchi{ARG1}{ARG2}
by
\chi^{ARG2}(ARG1)
For simple cases, ...
1
vote
2
answers
47
views
M-x replace-regexp uppercases words in replacement text based on the first letter present in the line
So, I have the following buffer:
1234
Red
blue
green
yellow
# colors
orange
Now, if I run M-x replace-regexp I get odd results. If I have the search string: ^\(.+\)$ and the replacement string: ...
3
votes
1
answer
189
views
Arithmetics in a regex in a lisp function
I want to replace all hard column widths in several LaTeX-documents, e.g. replace {90mm} by {0.75\textwidth}. I already found out that you can use \, and #1 to so arithmetics on captured groups, like ...
2
votes
1
answer
1k
views
"Invalid use of '\' in replacement text" with non-interactive calls
It seems that replace-regexp has a different behavior if called interactively and not, but I do not understand why, and how to get the wanted behavior.
What I want is the non-interactive equivalent ...
3
votes
1
answer
37
views
how can i do for replace a part of a regexp
For example i have this text:
select * from T1 ;
select * from T2 ;
select * from T3 ;
My objetive is replace the text by this other:
select * from T1_H ;
select * from T2_H ;
select * from ...
2
votes
2
answers
461
views
query-replace-regexp on specific lines
how does one do a query-replace-regexp to add a prefix string but not on EVERY line but rather just lines with text (or start with specific text?)
i know how to add a prefix with query-replace-regexp ...
3
votes
1
answer
3k
views
How do I lowercase (downcase) a group with replace-regexp?
I have some text that looks like this:
"CFName"
"CLName"
"CDOB"
"CGender"
I would like to get output where are of the text inside the double quotes is ...
3
votes
1
answer
1k
views
Regular expression matching group replacement not working
I want to convert
.waitForVisible('.someselector')
.click();
Into:
.waitAndClick('.someselector');
Automatically in a lot of files. So I wrote:
(defun hhaamm-replace-regexp-in-buffer (from to)
(...
2
votes
1
answer
2k
views
Programmatically replace all instances of regex with capture groups and sub-matches in buffer
It is clear to me how to programmatically replace all instances of regex in a buffer with some text, something like (pardon my naive code):
(defun my/regex-global-replace (regex subst-text)
"...