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.

Written in a declarative language, patterns for matching text use a combination of special characters and ordinary characters. Many Emacs commands can use for searching, matching, replacing, and navigating. Emacs has several methods of entering search patterns, including the interactive re-builder and alternative syntax styles. Regex versions of and commands can also work incrementally like the .

Some commands include regex or regexp prefixes or suffixes. Common ones are replace-regexp, query-replace-regexp, align-regexp, highlight-regexp. But not all commands have in their names, such as multi-occur, how-many, keep-lines, flush-lines, grep, lgrep, and rgrep. Emacs uses patterns extensively.

Questions tagged with or should include additional tags or clarify which command is being used for regex matching.

491 questions
42
votes
2 answers

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 string abc\n, $ matches the position between c…
Tim
  • 5,007
  • 7
  • 32
  • 61
20
votes
2 answers

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 this? Vim seems capable, but evil mode in spacemacs…
Heisenberg
  • 453
  • 4
  • 9
18
votes
1 answer

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
  • 3,903
  • 2
  • 24
  • 44
13
votes
1 answer

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 escaped. Is there a function for escaping these…
tmalsburg
  • 2,570
  • 1
  • 16
  • 30
9
votes
1 answer

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 remember correctly). After some investigation, I have…
Mark Karpov
  • 4,943
  • 1
  • 26
  • 54
9
votes
1 answer

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
  • 10,363
  • 2
  • 30
  • 76
8
votes
1 answer

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. ...pdf\\'"... What does it mean? What purpose…
brittAnderson
  • 729
  • 1
  • 8
  • 18
8
votes
1 answer

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 overflow for huge texts (37 KB). Is the overflow the…
Vroomfondel
  • 205
  • 1
  • 7
7
votes
1 answer

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 make them footnotes. The thing is, since that…
shackra
  • 2,782
  • 19
  • 49
6
votes
1 answer

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…
Onner Irotsab
  • 443
  • 2
  • 10
6
votes
1 answer

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 456 in test456test? What do I overlook,…
Tom Regner
  • 904
  • 1
  • 9
  • 13
6
votes
1 answer

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 equivalent in emacs? As far as I know, evil mode…
Toothrot
  • 3,264
  • 1
  • 13
  • 32
6
votes
1 answer

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 press C-s and type the regexp. When I'm looking in…
ReneFroger
  • 3,850
  • 23
  • 66
5
votes
1 answer

How close and exit from buffer *Re-builder*?

emacs 26.1 M-x regexp-builder Input some regexp. Nice. But now I want to close and exit from buffer Re-Builder. How I can do this?
a_subscriber
  • 4,062
  • 1
  • 18
  • 56
5
votes
1 answer

Whitespace and newlines in regexps?

For a number of reasons (one of them my lameness) I get extremely confused when it comes to whitespace and newlines in regexps. Say for example that I want to search for an equal sign (=) followed by any number of whitespace and newline…
Joakim G.
  • 51
  • 1
  • 1
  • 3
1
2 3 4 5 6 7 8