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
28
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 ...
6
votes
1answer
4k 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"
19
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
651 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 ...
2
votes
3answers
163 views

Elisp regex matching - evaluate as region vs as individual sexp gives different answers

Here's a chunk of code: (defvar test-string "bla bla bla 10.10.10.10 bla bla bla") (string-match "[[:digit:]]+\\.[[:digit:]]+\\.[[:digit:]]+\\.[[:digit:]]+" test-string) (message (match-string 0 ...
32
votes
2answers
6k 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-...
25
votes
2answers
239k 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 ...
14
votes
1answer
822 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 ...
5
votes
1answer
1k 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
2answers
221 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/...
3
votes
1answer
418 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 ...
0
votes
2answers
124 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
62 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 ...
16
votes
2answers
699 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 ...
12
votes
2answers
1k 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)...
6
votes
2answers
368 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
1answer
68 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 ...
3
votes
2answers
211 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 ...
2
votes
1answer
503 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 ...
2
votes
2answers
153 views

Regexp for matching quoted strings that may have control characters in them

I am trying to use a regexp to match strings, e.g. a pattern that matches these kinds of quoted strings: "test" or 'test' or 'test with "quote"' or "test with 'quote'" This pattern matches all those,...
2
votes
1answer
40 views

Why is the order of : and - important in a regexp character class

Defining the following function (defun windows-path-backwards () (interactive) (skip-chars-backward "a-zA-Z0-9\\\\_-:")) and running it while standing at | in the below text C:\this\path| ...
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 "[[:...
1
vote
2answers
404 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
384 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-...
4
votes
3answers
1k 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
195 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
1answer
348 views

Repeated regex capture for font-lock

Let's say I have a language which looks like this: anchor (item something item some thing item some item thing) item item and that I'm playing with font-lock a bit. I would like to highlight "anchor"...
2
votes
1answer
49 views

How to replace a digit by a letter?

I want to replace some digits by letters such that 1 ---> a 2 ---> b ... In emacs I have tried the following replacement: M-x query-replace-regexp RET \([0-9]+\) RET \,(string (+ ?a \#1)) RET ...
2
votes
3answers
624 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,\}...
0
votes
0answers
57 views

Regex search and edit text simultaneously with multiple cursors

I'd like to search for a pattern with regex and simultaneously cut or edit all instances of that pattern. Is there a ready-made function for this? Atom, for example, has one such package - by ...
0
votes
1answer
208 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 (...