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.

475 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
  • 4,987
  • 7
  • 31
  • 60
36
votes
2 answers

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-regexp. Here are my questions: Does the first…
Just a learner
  • 547
  • 5
  • 11
34
votes
1 answer

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 to build directly usable expressions, so what…
user2699
  • 2,181
  • 16
  • 32
24
votes
6 answers

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(?:[./#\+-]\w*)+")) Without the fancy macro, it's just…
nanny
  • 5,704
  • 18
  • 38
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
  • 433
  • 4
  • 9
19
votes
3 answers

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 are ` and ' characters available. Based on the…
Jackson
  • 1,218
  • 9
  • 19
18
votes
2 answers

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 from Wikipedia): (?<=\.) {2,}(?=[A-Z]) would match…
Amelio Vazquez-Reina
  • 5,157
  • 4
  • 32
  • 47
18
votes
2 answers

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? Looking for dam. Result: dam madam dam 1 dam 2 dam…
MatthewRock
  • 1,453
  • 13
  • 27
16
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,773
  • 2
  • 21
  • 40
16
votes
2 answers

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) AC(5) AC(6) AC(7) AC(8) AC(9) AC(10) I used…
Prasanna
  • 1,470
  • 16
  • 30
16
votes
1 answer

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 rather avoid using eval. I've found rx-to-string,…
Wilfred Hughes
  • 6,890
  • 2
  • 29
  • 59
15
votes
2 answers

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
  • 795
  • 6
  • 9
13
votes
3 answers

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 "bar" starting with the first line containing…
Larry Coleman
  • 1,465
  • 2
  • 11
  • 10
12
votes
7 answers

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 expressions need to be learned and practiced. Can I…
Felix Hoffmann
  • 765
  • 1
  • 7
  • 17
12
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,540
  • 1
  • 14
  • 29
1
2 3
31 32