I'm trying to use emacs to convert strings of the form "$$...$$" into "\[...\]". I just started learning to use emacs for this specific purpose for editing some files.
Following the first example of replace-regexp on emacs wiki, I intitially tried:
M-x replace-regexp
Replace regexp: \$\$.*\$\$
Replace regexp with: \,("\[" \1 "\]")
but I received the error: invalid function "[". I instead tried
Replace regexp with: "\["\1"\]"
but I received the error: Invalid use of `\' in replacement text.
Since neither of those approaches worked, I tried to adapt the 13th example of replace-regexp on emacs wiki, writing
M-x replace-regexp
Replace regexp: \$\$
Replace regexp with: \,(if (evenp \#) "\[" "\]")
but I received the error: (void-function evenp). Any suggestions? I'd like to understand what went wrong in each of these instances, and how to fix them.
You don't need macros.
(replace-regexp "\\$\\$\\(.*?\\)\\$\\$"
"\\\\[\\1\\\\]")
Or
Replace regexp: \$\$\(.*?\)\$\$
Replace with: \\[\1\\]
UPDATE
Used the greedy syntax .*? for the cases where there are multiple occurrences on a line (from #phil's comment below).
Note that this does not work when the text between $$ is across multiple lines. If the text is spread across multiple lines the following should work, though at that point I might just prefer rolling my own function:
(replace-regexp "\\$\\$\\(\\(.\\|\n\\)*?\\)\\$\\$"
"\\\\[\\1\\\\]")
Or
Replace regexp: \$\$\(\(.\| C-qC-j \)*?\)\$\$
Replace with: \\[\1\\]
Related
I have a bunch of column names from a SQL query, and I want to get rid of everything before the AS using Emacs. In other words, I want to go from
MAX(CASE WHEN maintenance.work_order IS NULL THEN 1 ELSE 0 END) AS Has_work_order,
to
Has_work_order,
I used re-builder to create a simple regex: "\.\*AS " which highlights the appropriate parts of the buffer. However, when I select the entire buffer and run query-replace-regexp using M-x query-replace-regexp <RET> "\.\*AS " <RET> "" <RET>, Emacs displays a Replaced 0 occurrences message.
What am I doing wrong?
By using re-builder (which is a good idea) to create a regexp for interactive use, you are then getting confused between the different regexp syntax options. re-builder defaults to read syntax (which you would use when writing elisp code), whereas for interactive use you want string syntax.
Refer to Why do regular expressions created with the regex builder use syntax different from the interactive regular expressions? for explanation and clarification.
In read syntax, \.\*AS represents the regexp .*AS (because . and * are not special when reading strings, so those backslashes are redundant); but in string syntax \.\*AS is the regexp \.\*AS in which the . and * characters which are special to regexps have been escaped, and therefore lose their special meaning, and will instead match literal . and * characters in the text.
Note, however, that when entering a regexp interactively you should not include the surrounding double-quote characters " that are present in re-builder even for its string syntax mode. If you enter the " characters interactively, then the regexp will be matching text that contains those " characters.
I was able to do this with the following:
M-x query-replace-regexp <RET> \(.+\)AS <RET> <RET>
Note the cursor must be above the line(s) that need replacing. I've not used this before, but it's interactive (pressing 'y' for each replace, this may be able to be done automatically/globally, but I've not played around with it.
I've wanted to get an overview of my Python program, so I ran:
M-x occur
and for the regexp I've supplied
(def)|(class)
which failed to match anything.
I've also looked at this post, and tried
(def)\\|(class)
but this failed to match anything either...
How do I get M-x occur to match class or def?
You have to use single backslash (without parentheses, or you should escape parentheses as well):
def\|class
I bother you to have some tips for this problem: I'm working in Latex with a very dirty code, generated by writer2latex (quite good programme, anyway) and, using Emacs, I'm trying to query-replace multiple lines of code, for instance:
{\centering [Warning: Image ignored] % Unhandled or unsupported graphics:
%\includegraphics[width=11.104cm,height=8.23cm]{img34}
have to become:
\begin{figure}[tpb]
\begin{center}
\includegraphics[width=\textwidth]{img34}
Using M-x re-builder, I found out that I could underline the whole region I need to query-replace with the string: \{.*centering.*c-qc-j.*cm] but, if I M-x replace-regexp using this, I only get: Invalid regexp: "Invalid content of \\{\\}"
Any suggestion about how to perform the query? I have a HUGE amount of lines like these to replace... :-)
You're getting this error message because in Emacs' regular expressions the curly braces\{ and \} have special meaning. These braces are used to specify that the part of the regexp immediately before the braces should be matched a certain number of times.
From the GNU Emacs documentation on regexps:
\{n\}
is a postfix operator specifying n repetitions [...]
\{n,m\}
is a postfix operator specifying between n and m repetitions [...]
If you want your regexp to actually match a curly brace, do not escape it with a leading slash:
{.*centering.*C-q C-j.*cm]
In order to use a backslash in the replacement string you have to escape it with another backslash. (When doing this in code, it quickly becomes quite ugly because inside a double-quoted string backslashes themselves have to be escaped already. However, since you are doing your replacements interactively, the double escaping is not necessary and thus two backslashs are enough.)
M-C-% {.*centering.*C-q C-j.*cm] RET \\begin{figure}[tpb]C-q C-j\\begin{center}C-q C-j\\includegraphics[width=\\textwidth] RET
Make sure the re-syntax is "read", C-c tab. Remove the initial backslash. Now the regexp should work if you yank it into replace-regexp
How do you do a query-replace-regexp in Emacs that will match across multiple lines?
as a trivial example I'd want <p>\(.*?\)</p> to match
<p>foo
bar
</p>
M-x re-builder
is your friend. And it led me to this regular expression:
"<p>\\(.\\|\n\\)*</p>"
which is the string version of
<p>\(.\|^J\)*</p> ;# where you enter ^J by C-q C-j
And that works for me when I do re-search-forward, but not when I do 'query-replace-regexp. Unsure why...
Now, when doing a 're-search-forward (aka C-u C-s), you can type M-% which will prompt you for a replacement (as of Emacs 22). So, you can use that to do your search and replace with the above regexp.
Note, the above regexp will match until the last </p> found in the buffer, which is probably not what you want, so use re-builder to build a regexp that comes closer to what you want. Obviously regular expressions can't count parenthesis, so you're on your own for that - depends on how robust a solution you want.
Try character classes. As long as you're using only ASCII character set, you can use [[:ascii:]] instead of the dot. Using the longer [[:ascii:][:nonascii:]] ought to work for everything.
suppose i have the following in a text file (or dired buffer) open in emacs:
file_01.txt
file_02.txt
...
file_99.txt
I want to query-replace or replace the files to
01_file.txt, etc.
I want to use query-replace-regexp or replace-regexp, but don't know what to put in. The search part i put in "file_..", but the ".." are read as periods in the replacement string. I'm beginning to learn regexp and don't know how to do this. Please help, thanks.
M-x replace-regexp invokes the function to replace with regular expressions.
For Replace regexp enter: \(file\)_\([0-9]+\)
This will create two groups, one that matches the 'file' part, and one that matches the number. The braces \( ... \) are necessary to make the match available later in the replacement string.
For Replace with enter: \2_\1
This inserts the second match from the search string (the numeric part), adds the _ (underscore) and then adds the first match from the search string (the 'file').
For more information on Emacs' regular expressions, see Regexp Syntax and Regexp Replace.
Once you have mastered the regexp basics you might want to check out the Emacs ReBuilder tool with M-x re-builder, which lets you build regexes interactively.