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
42 votes
2 answers
460k 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 ...
Tim's user avatar
  • 5,007
36 votes
2 answers
10k 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-...
Just a learner's user avatar
35 votes
1 answer
6k 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 ...
user2699's user avatar
  • 2,231
24 votes
6 answers
10k 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(...
nanny's user avatar
  • 5,756
21 votes
2 answers
5k 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? ...
MatthewRock's user avatar
  • 1,483
20 votes
2 answers
5k 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 ...
Heisenberg's user avatar
19 votes
3 answers
2k 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 ...
Jackson Ray Hamilton's user avatar
18 votes
1 answer
10k 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"
american-ninja-warrior's user avatar
18 votes
2 answers
948 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 ...
Amelio Vazquez-Reina's user avatar
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)...
Prasanna's user avatar
  • 1,540
16 votes
1 answer
1k 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 ...
Wilfred Hughes's user avatar
15 votes
2 answers
652 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\\)"
nosefrog's user avatar
  • 805
13 votes
7 answers
2k 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 ...
Felix Hoffmann's user avatar
13 votes
4 answers
4k 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 "...
Larry Coleman's user avatar
13 votes
1 answer
6k 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 ...
tmalsburg's user avatar
  • 2,570
11 votes
3 answers
4k 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" ...
user23847's user avatar
  • 111
11 votes
2 answers
2k 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?
izkon's user avatar
  • 1,878
11 votes
1 answer
899 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 ...
Geradlus_RU's user avatar
11 votes
1 answer
4k 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 ...
user avatar
10 votes
1 answer
3k 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 ...
doctorate's user avatar
  • 1,829
9 votes
1 answer
592 views

How does Emacs compile the regular expressions?

I'm writing code that will have to do a lot of regular expression matching. I understand that a function like string-match or re-search-forward needs to process its regexp argument given as a string ...
phs's user avatar
  • 1,227
9 votes
1 answer
2k 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?
PythonNut's user avatar
  • 10.4k
9 votes
1 answer
1k 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 ...
Jon Ericson's user avatar
9 votes
1 answer
3k 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 ...
Mark Karpov's user avatar
  • 4,953
9 votes
2 answers
3k 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 ...
Malabarba's user avatar
  • 23.2k
8 votes
3 answers
2k 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 ...
rl1's user avatar
  • 356
8 votes
1 answer
1k views

How to apply arithmetic operators to query-replace-regex

I am attempting to replace all "begin" and "dur" attribute values in my SVG code with one-tenth of (or ten times) their current values, in order to speed up/slow down animation for testing/debugging ...
Peter Chaplin's user avatar
8 votes
3 answers
2k 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 ...
Sean Allred's user avatar
  • 6,921
8 votes
1 answer
1k 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. ....
brittAnderson's user avatar
8 votes
3 answers
870 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 ...
doctorate's user avatar
  • 1,829
8 votes
1 answer
404 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 ...
Vroomfondel's user avatar
7 votes
1 answer
617 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?
serghei's user avatar
  • 272
7 votes
3 answers
33k 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 ...
erjoalgo's user avatar
  • 863
7 votes
2 answers
363 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)?
Jessie's user avatar
  • 327
7 votes
2 answers
590 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 ...
ideasman42's user avatar
  • 8,796
7 votes
1 answer
463 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 ...
shackra's user avatar
  • 2,782
7 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 ...
Matt Messersmith's user avatar
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 ...
hrkrshnn's user avatar
  • 439
6 votes
3 answers
982 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 ...
Matthew Gilliard's user avatar
6 votes
1 answer
1k 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 ...
Tim's user avatar
  • 5,007
6 votes
1 answer
2k 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 ...
ReneFroger's user avatar
  • 3,870
6 votes
1 answer
1k views

How can I use more than 9 regex capture groups in Emacs Lisp?

I have to do a regexp replacement with more than 9 capture groups. How can I do it? Here there is my code (with \\10 and \\12 that they do not work as I expected to): (perform-replace "\\\\href{...
Onner Irotsab's user avatar
6 votes
1 answer
416 views

Regexp replace without interaction

Is it possible to replace regex like query-replace-regex without asking for each matching ?
user3593232's user avatar
6 votes
1 answer
61 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, ...
Name's user avatar
  • 7,859
6 votes
1 answer
79 views

Matching categories '\cX' and digit

The regex info material suggests that \\c6 should match digit characters. In UTF-8 encoded buffers it doesn't; some of the other category letters work as expected (l, |, a). Shouldn't \\c6* match ...
Tom Regner's user avatar
6 votes
1 answer
185 views

How can I fontify a symbol by regexp match group (as in a naming convention)?

I want to add better support for expl3 to AUCTeX. I have regular expressions that properly group according to the naming conventions for: functions (\\(?:__)?[a-z]+_)([a-z_]+)(:[nNpTFwcVvxof]*), ...
Sean Allred's user avatar
  • 6,921
6 votes
1 answer
588 views

Equivalents to vim \zs and \ze?

In vim, all instances of an apple tree can be replaced by an orange tree with :%s/an \zsapple\ze tree/orange/g. \zs and \ze mark the start and end of the passage to be operated on. Is there something ...
Toothrot's user avatar
  • 3,264
5 votes
4 answers
481 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?
knarf's user avatar
  • 323
5 votes
2 answers
25k 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?
stacko's user avatar
  • 1,597
5 votes
1 answer
6k views

Is there an equivalent of string-match for regexp?

I'd like to write something like this except to compare the region with a regexp. (if (string-match (buffer-substring-no-properties (point) (mark)) "foo") (bar)) Is there an ...
stacko's user avatar
  • 1,597

1
2 3 4 5
10