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
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-...
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 ...
20
votes
2answers
171k 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 ...
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(...
16
votes
3answers
999 views

Elisp regexps ^ and $ vs ` and '

The manual describes the regexp special characters ^ and $. Like in most regular expression dialects I know, they seem to match the start or end of a string. However, I've also discovered that there ...
15
votes
2answers
600 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 ...
15
votes
1answer
715 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 ...
14
votes
2answers
473 views

Does elisp have regexp literals?

The sheer number of backslashes my regexps require is pretty crazy. Does elisp have regexp literals, so I can write something like rx"some\(regexp\)" instead of "some\\(regexp\\)"
14
votes
3answers
3k views

Is there an equivalent to sed-style substitution commands from vim?

One of the things I miss from vim is being able to type in a substitution command that will work over multiple lines, for example: :/begin/,/end/s/foo/bar/g The above command substitutes "foo" for "...
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 ...
12
votes
2answers
885 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)...
12
votes
1answer
2k views

support for regex look behind and ahead?

I need to perform regex query replace, such that foo in foo bar is matched, but foo in foo baz is not. Normally I would use regex look ahead, e.g. foo(?=bar). However, it seems like Emacs cannot do ...
11
votes
1answer
2k views

How to search an Arabic word in text without its diacritics/accents?

In Arabic as with some other languages there is what is called diacritics to enhance pronunciation. There is no convention on how many diacritics should be written for a single word. Some use the ...
10
votes
1answer
468 views

`looking-back` performance

I have some code using (looking-back … (line-beginning-position)). Doc string of looking-back states that it's better to avoid this function because of slowness. I'm curious will following approach ...
9
votes
1answer
1k views

Regex match literal string

I want to write a function (or use an existing one) that will take any string and produce a correctly escaped Regex that matches only that string. What is the fastest and simplest way of doing this?
9
votes
1answer
723 views

Align vertical columns of numbers on the decimal point

I wrote this post in Emacs. In order to make the tables look nice, I decided to adjust the spacing so that decimals line up neatly. The original format from the database looked like this: emacs ...
9
votes
1answer
2k views

Evil mode and regular expressions

In Vim, if I want search for all variable names LX where X is a number, I can simply type /L\d. However, this doesn't seem to work with evil. Is there another syntax for regular expressions in Evil ...
8
votes
2answers
454 views

Search through the values of all variables in Emacs

I'd like to find which variables contain a certain value. Is there a way to do a regex search through the values of every variable in Emacs?
8
votes
1answer
821 views

What does a backslash followed by a single quote mean in a regular expression?

Looking at this stackoverflow answer on an old way for specifying the org-file-apps list, I can see that there is a backslash followed by a single-quote character after each file type extension, e.g. ....
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 ...
8
votes
1answer
321 views

Most performant matching of “any char”

On https://www.emacswiki.org/emacs/MultilineRegexp one finds the hint to use [\0-\377[:nonascii:]]*\n instead of the standard .*\n to match any character up to a newline to avoid stack ...
8
votes
1answer
3k views

How to escape regexp special characters in a string?

I'm composing a complex regular expression and the user can supply a part of the expression. However, the user-supplied part should be interpreted literally, i.e. regexp special characters should be ...
8
votes
1answer
2k views

How to match symbol in regexp?

I'm trying to construct a regular expression that would match symbols, that is, things like function names and the like, according to current major mode settings (it's called syntax table if I ...
7
votes
1answer
522 views

Is there any principal difference between “A-Z” and upper?

I just want to make sure I understood this correctly. (rx (one-or-more (any upper lower))) is equal to (rx (one-or-more (any "A-Z" "a-z"))) Correct?
7
votes
2answers
2k views

Only show lines containing phrase/regex

I would like to see all the lines from the current buffer, that contain some phrase, or match regex. Example: My buffer: dam madam madam this is da m a dam 1 dam 2 dam pi dam 321:) is dam 2? ...
7
votes
2answers
300 views

When to use normal incremental search in place of the regex version?

Assuming familiarity with regex, is there ever any reason to use the normal incremental search (C-s) over regex (C-M-s)?
7
votes
1answer
1k views

In regular expressions, what's the difference between [:blank:], [:space:], and \s-?

I've been playing around with rx lately, and I just noticed there are (at least) 3 different ways of indicating whitespaces in regular expressions. [:blank:] [:space:] \s- What are the differences ...
6
votes
3answers
734 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 ...
6
votes
2answers
1k views

What's the idiomatic (or best) way to trim surrounding whitespace from a string?

I'm working with strings which may have any number of prefix and suffix spaces, tabs, newlines, etc. Currently I have this: (replace-regexp-in-string "^[^[:alnum:]]*\\(.*\\)[^[:alnum:]]*$" "\\1" ...
6
votes
3answers
536 views

highlight-regexp in multiple buffers

highlight-regexp is pretty amazing, but when I run it manually by M-x highlight-regexp RET f?o RET 'someface it only applies the highlight to the current buffer. How can I apply a highlight to ...
6
votes
3answers
761 views

How to grep marked files in the dired mode of emacs?

I hope it's a simple problem.. I'd like to search for the regexp 'thisWord' only in the marked files of a dired buffer. Usually I used in the dired buffer: M-x grep and then in the mini-buffer grep ...
6
votes
2answers
253 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 ...
6
votes
1answer
665 views

Regexp replacement: difference between \([[:digit:]]+\) and ([[:digit:]]+)

With M-x replace-regexp, I found that both \([[:digit:]]+\) and ([[:digit:]]+) will match (123), and not one without parentheses, But the first will replace 123 and the second (123). I wonder why the ...
6
votes
2answers
19k views

How to make a dot match a newline

Often I need to use regular expressions to match strings across multiple lines. In python and other languages, it's possible to ask that dots match newlines (as opposed to the default behavior). Is it ...
6
votes
1answer
340 views

Using the count of coincidence from a regexp as replacement text?

I have an org-mode file with more than 12000 footnotes (maybe less), I was cleaning it and found that after the conversion some footnotes weren't converted to org-mode footnotes, so I had to manually ...
6
votes
1answer
54 views

How to check with regular expressions or another way if some package is used in a LaTeX document?

How to check if a specific package is used by a LaTeX document. For example in the below document (intentionally written in an ugly but technically correct format), the packages fontenc, inputenc, ...
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"
5
votes
2answers
13k views

Escaping brackets in regexp

I tried these two, but none of them worked. (if (looking-at "\[") (insert "f")) (if (looking-at "[") (insert "f")) How can I escape square brackets in emacs regexp?
5
votes
4answers
267 views

Is there a way to search/replace with regexps interactively using rx syntax?

I really like rx but I can't use it interactively. Has someone already tried to extend isearch to support rx syntax?
5
votes
2answers
199 views

How to detect if the point is at a closing delimiter?

How to detect if the point is at a closing delimiter? For example: in Elisp: ) in LaTeX: }
5
votes
2answers
3k views

Matching multiline comments in regex

I'm writing a major mode for a language that uses (* ... *) as comments. I'm trying to match multi-line comments. e.g. (* foo bar *) I was initially doing "(\\*.*\\*)", however, . doesn't match ...
5
votes
1answer
932 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_ --&...
5
votes
2answers
1k views

in Emacs regex, how to search for uppercase letters only?

I'm trying to build a function but having trouble with the regex. Specifically, I want to match capital letters only. This function is supposed to capitalize words that are in lowercase (the → The) ...
5
votes
1answer
192 views

How to use the regexp for digits with an interactive search?

For example, I have the following content in the buffer: 123 1234 12345 123456 1234567 To match the 4 numbers, I need the regular expression of \d{4}. I would like to do the same in Emacs, when I ...
5
votes
2answers
228 views

Replace a two-line string not ending in digit with a one line string

I would like to replace a two-line string not ending in digit with a one line string. For example, replacing the following two-line string: Extensions of the Single—Sa1e Model 414 With a one line ...
5
votes
1answer
374 views

replace-regexp: pattern is matching too much

I'm having problems to replace a URL inside a HTML tag with replace-regexp. This is the original string: <img src="http://localhost/~user/foo.bar/place/foo_bar.jpg" ... attr="..." /> This is ...
5
votes
1answer
307 views

Can occur match phrases that stretch across multiple lines?

Q: (how) can I use occur to match a phrase that stretches over multiple lines? Consider the following buffer: Here's a line of text with the phrase "kittens and puppies". Here's an awkward ...
5
votes
3answers
491 views

how to use CSS to change styling of elements in org-mime emails?

The org-mime page describes how to use org-mime-html-hook to style HTML email by inserting inline CSS. I used this method to successfully change the styling of my body paragraphs by targeting <p>...
5
votes
2answers
800 views

How can I build a regexp insensitive to case?

I have a bunch of keywords: set write quit which each can be shortened down to one letter: s se set w wr wri writ write q qu qui quit Further, these keywords are case-insensitive, so the following ...
5
votes
1answer
1k views

Why are double quotes not escaped in the replace-regexp command?

I have text like this: myvariable = ABCD, EFGH, IJKL, ~subsection1 MNOP, QRST, UVWX, YZ Which I want to transform to: myvariable = "ABCD," "EFGH,\n" \ "IJKL,\n" \ "~subsection1\n" \ "MNOP,\n" \ "...