Questions tagged [regular-expressions]

is for patterns specifying search or replace strings. Also known as `regex` or `regexp`, the patterns describe strings to match when searching or replacing. Emacs provides extensive support for `regex` patterns in many commands. Emacs also provides an interactive expression builder for such patterns.

Filter by
Sorted by
Tagged with
26
votes
1answer
4k views

Why do regular expressions created with the regex builder use syntax different from the interactive regular expressions?

So, using the regular expression builder (M-x re-builder), finding lines that end in \ takes "\\$", while in search and replace by regex, it only takes "\$". I would have expected the regex builder ...
5
votes
1answer
2k views

how do I quickly remove lines from emacs buffer

In an emacs buffer when editing a file called "log/development.log" how do I quickly remove all lines containing the word "Render"
18
votes
6answers
6k views

Get all regexp matches in buffer as a list

On the Code Golf Stack Exchange site today, I found this answer in Clojure to the question "Get all links on a webpage". (->> (slurp "http://www.stroustrup.com") (re-seq #"(?:http://)?www(...
8
votes
3answers
575 views

Search and replace through a multifile project in AUCTeX?

I am about to adopt Emacs to be the only editor I use in all what I am writing including LaTeX documents. I came from WYSIWYG editor which served that purpose fairly nice, namely TexStudio or TXS for ...
31
votes
2answers
5k views

Understanding of emacs align-regexp

I've read the Emacs documentation for align-regexp but still have difficulty in understanding how it works. What I'm talking about is its prefixed form C-uM-xalign-regexp, not the simple form M-xalign-...
19
votes
2answers
169k views

What is the regex to match a newline character?

In Emacs regex, \n doesn't match a new line character \n. Am I correct that $ matches the position between a new line character and the character right before the new line character. e.g. for a ...
15
votes
1answer
711 views

How do I create a dynamic regexp with rx?

I want to use rx to create regular expressions with runtime values. Currently, I'm doing this: (setq strings '("foo" "bar" "baz")) (eval `(rx symbol-start (or ,@strings) symbol-end)) However, I'd ...
12
votes
7answers
1k views

How can I practice searching and replacing with regular expressions interactively in Emacs?

Searching and replacing using regular expressions is clearly a powerful tool to the Emacs user who can routinely perform these actions. However, as an Emacs (and/or programming) beginner, regular ...
3
votes
1answer
311 views

Find and remove consecutive duplicated words while ignoring case

I am trying to find and remove duplicated words in a LaTeX file. Right now I am using query-replace-regexp with the regular expression \b\(\w+\)[ #]+\1 where # stands for line-break (C-q C-j). This ...
3
votes
2answers
183 views

Global goto regex?

I would like to run a command and go to the first matching regex pattern that matches in any open buffer. Kind of like search-forward-regex. The requirements are: not be limited to forward/...
0
votes
2answers
114 views

Regexp for distinguishing uppercase and lowercase not working

I've made the following command: (defun search-backward-lowercase-alphabet () (interactive) (search-backward-regexp "[a-z]")) But when I run it at the end of a line like the following: abcDEF ...
0
votes
0answers
55 views

Has matches , but re-builder print “No matches”

Emacs 26.1 In scratch buffer I has text KRW-EOS hel-how hello-how-ar h-privet kak-del 123-567 aaa-bbb AAA-BBB AAAA-BBBB AAA-BB AA-BBB A-A AAAAAAAAAA BBBBBB AAA BBB ZZZ-YYY M-x regexp-builder Input ...
15
votes
2answers
593 views

How can I test and use a Perl regular expression interactively?

Say I have a buffer with text. I would like to test a regular expression (ideally Perl type) against my buffer and have Emacs highlight the matches on it. For example, the following regexp (taken ...
6
votes
2answers
252 views

How to transpose two arguments of a function call for C family languages?

How to swap two arguments for C function calls? eg: my_function(foo, bar, baz); ~~~~~~~~~~~~~^ (cursor location) Should be transposed to the right to make: my_function(bar, foo, baz); The simple ...
3
votes
2answers
144 views

How can I select only the content of a @media in CSS with regexp in Emacs?

I am trying to select the content of @media in CSS with regexp in Emacs (elisp) to make a slight change in the color of the background or font, so I can easily see that I am editing inside of them. I ...
3
votes
1answer
64 views

Emacs regex replace while re-using part of the expression in the replacement

I sometimes need to do replacements which would involve using matched parts for the output. For example, I would like to replace varName->doWork() with ::doWork(varName, 42) whatever varName ...
1
vote
2answers
216 views

How to extract some text from within HTML code?

I have this string in an elisp variable <br /><b>Country</b>: United States<br /><a href I want to extract out the United States in to a separate variable. How can I do ...
1
vote
2answers
327 views

Regexp to font-lock numbers but not strings?

I would like to have my dollar sign yellow coloured. So after searching around, I learnt that the Emacs regex's are different from default regexs. After a while, I got this worked: (defface font-...
1
vote
2answers
1k views

Named groups for query-replace-regexp

I'd like to define a function to do a query-replace-regexp with a pattern I often use. When not using capture groups, it works: (defun my-replace () (interactive) (query-replace-regexp "[[:...
5
votes
1answer
928 views

How to save part of a regular expression during search and replace?

I think the best way to explain what I'm trying to do is using an example. Suppose one would want to replace all underlined words with italic ones in org-mode using a regular expression: _example_ --&...
4
votes
3answers
919 views

Search for and correct capitalized words that are not at the beginning of a sentence

I have a latex document in which there are many incorrectly Capitalized words. Could anybody suggest a clever regex to track them? Even better if there is a function which cycles through such words ...
3
votes
2answers
162 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, ...
2
votes
3answers
419 views

Why doesn't “$^J” match a new line character after another line?

I want to match two or more consecutive lines with numbers only, e.g.: abc 1 2 abc In Emacs, my regex is \(^ *[[:digit:]]+ *$^J\)\{2,\}, but it doesn't match any. I find \(^ *[[:digit:]]+ *^J\)\{2,\}...
2
votes
2answers
115 views

re-search-forward unexpected behavior: case insensitive

According to the documentation of this function, it should be case sensitive. But when I test it out, the result is case insensitive. My emacs version is 25.3.2.
1
vote
1answer
393 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 ...
0
votes
1answer
137 views

How to use flush-lines in a script?

I want to use flush-lines in a selected region, but I have troubles. Here it is my code: (defun delete-empty-lines () "Delete empty lines in bibliography." (interactive) ;; set point-min (...