36
votes
Accepted
Understanding of emacs align-regexp
OK, first the alignments and then the explanation of how it works.
To align the first one, select the lines, do C-u M-x align-regexp and choose: \(\s-*\):, 1, 1, and y.
For the second, use ,\(\), 1, ...
36
votes
Accepted
Why do regular expressions created with the regex builder use syntax different from the interactive regular expressions?
There are actually four different re-builder syntax options, and you can switch between them with C-cTAB
Two are for the sexp-form regexp compilers rx and sregex (but as the former is more ...
29
votes
What is the regex to match a newline character?
Yes. $ matches the end of the line, not the newline character which comes after the end of the line.
Do C-M-s C-q C-j. C-q is the default binding for quoted-insert and works in the minibuffer too. ...
24
votes
Accepted
Only show lines containing phrase/regex
...see all the lines from the current buffer...
With built-in commands and no external packages or dependencies in
a new buffer, use:
M-x occur
the same buffer, use:
M-x keep-lines
21
votes
Accepted
how do I quickly remove lines from emacs buffer
You can go to beginning of buffer with M-<, then M-x flush-lines, type your word and hit RET.
(flush-lines REGEXP &optional RSTART REND INTERACTIVE)
Delete lines containing matches for ...
19
votes
Accepted
Get all regexp matches in buffer as a list
Here is how you can do it based on strings, as requested.
(defun re-seq (regexp string)
"Get a list of all regexp matches in a string"
(save-match-data
(let ((pos 0)
matches)
...
17
votes
How can I practice searching and replacing with regular expressions interactively in Emacs?
Besides regexp-builder you might also consider visual-regexp to provide you with visual feedback on the replace in progress:
17
votes
What is the regex to match a newline character?
As Dan comments, the regex that matches a newline is a newline.
You can represent a newline in a quoted string in elisp as "\n". There is no special additional regexp-specific syntax for this -- you ...
16
votes
How can I practice searching and replacing with regular expressions interactively in Emacs?
You can try M-x regexp-builder RET which is an interactive regular expression matcher, that's not bad
16
votes
Accepted
How can I test and use a Perl regular expression interactively?
There are two nice packages for working with perl kind of regexes effectively, which should be used together - visual-regexp and it's extension - visual-regexp-steroids. They are great addition to ...
16
votes
Accepted
How do I create a dynamic regexp with rx?
rx-to-string takes a regexp form as an argument. The syntax is the same as the argument of rx.
(rx-to-string '(or "foo" "bar"))
"\\(?:\\(?:bar\\|foo\\)\\)"
What you tried passing is not a regexp ...
16
votes
Accepted
Elisp regexps ^ and $ vs ` and '
Your string might have an embedded newline character, in which case \' matches the end of the string but $ matches just before the newline char.
To quote the Elisp manual, node Regexp Special:
...
15
votes
Accepted
Search through the values of all variables in Emacs
Does apropos-value do what you're looking for?
(apropos-value PATTERN &optional DO-ALL)
Show all symbols whose value’s printed representation matches PATTERN.
PATTERN can be a word, a list of ...
15
votes
Accepted
Incrementally replace a given string
General technique
Your replacement string can contain arbitrary lisp code. From the documentation for replace-regexp:
In interactive calls, the replacement text may contain ‘\,’ followed by a Lisp ...
14
votes
Accepted
How to escape regexp special characters in a string?
You can use regexp-quote:
This function returns a regular expression whose only exact match is string. Using this regular expression in looking-at will succeed only if the next characters in the ...

Dan♦
- 31k
13
votes
Get all regexp matches in buffer as a list
It's probably worth noting that invoking occur with the universal argument causes it to populate the *Occur* buffer with only matches — no file names, line numbers or header information. When combined ...
13
votes
Accepted
Does elisp have regexp literals?
One option is to use the rx macro to construct your expressions using sexps.
Your example becomes (rx "some" (group "regexp"))
Here are a couple more examples from the commentary section in rx.el, ...
13
votes
support for regex look behind and ahead?
No, Emacs regular expressions do not support arbitrary zero-width look-ahead/behind assertions.
n.b. Evil and Spacemacs (like all elisp libraries) are irrelevant when it comes to questions about the ...
13
votes
Accepted
Is there any principal difference between "A-Z" and upper?
The macro rx returns regexp strings that can be passed to other Emacs functions.
ELISP> (rx (one-or-more (any upper lower)))
"[[:lower:][:upper:]]+"
ELISP> (rx (one-or-more (any "A-Z" "a-z")))
"...
11
votes
Accepted
Regexp replacement: difference between \([[:digit:]]+\) and ([[:digit:]]+)
In emacs regular expressions (unlike most regexp engines), \( and \) are group delimiters, while ( and ) match litteral brackets.
So: \([[:digit:]]+\) matches one digit or more, that is here 123, and ...
11
votes
Accepted
What does a backslash followed by a single quote mean in a regular expression?
It's a special construct in emacs regexp that matches the end of a string (not just the end of a line). Quoting the the manual
\'
matches the empty string, but only at the end of the string or ...
11
votes
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
The built-in library subr-x.el has included the inline functions string-trim-left, string-trim-right, and string-trim ...
10
votes
Is there an equivalent to sed-style substitution commands from vim?
Here is one way of doing it that uses built-in functionality only:
With point in the line that contains first occurrence of begin, press C-SPC.
Move to next occurrence of end:
C-s end RET
Replace ...
10
votes
Accepted
How to save part of a regular expression during search and replace?
Use \(...\)for grouping and \1 to reference the first saved group (\2 for the second, all the way up to \9). E.g.:
query-replace-regexp: _\([^_]+\)_ into /\1/.
See Regexp Backslash in the Emacs Lisp ...
10
votes
Get all regexp matches in buffer as a list
I have an emacs lisp answer to that question posted: https://codegolf.stackexchange.com/a/44319/18848
Using the same (while (search) (print)) structure you could modify it into a function to push ...
10
votes
Accepted
`looking-back` performance
Definitely. You will especially gain if you just want to test char-before or search backward for a literal string. And if you must use looking-back then try to use a LIMIT argument, if possible.
See ...
9
votes
Accepted
Regex match literal string
You are looking for regexp-quote:
This function returns a regular expression whose only exact match is string. Using this regular expression in looking-at will succeed only if the next characters ...

Dan♦
- 31k
9
votes
Accepted
Most performant matching of "any char"
In Emacs's regexps, . does not match all characters. It is a synonym of [^\n]. So the reason for using [\0-\377[:nonascii:]] is when you want to match "any char, even a newline".
W.r.t overflowing ...
9
votes
Accepted
Regexp replace to match a string, but not match a superstring
Try \_<Vector\_>. The \_< construct matches the empty string, but only at the beginning of a symbol. \_> is the same, but at the end of a symbol. What is a "symbol" depends on the buffer's ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
regular-expressions × 433replace × 54
search × 43
font-lock × 26
query-replace × 21
org-mode × 18
string × 18
rx × 12
query-replace-regexp × 11
dired × 10
elisp × 9
isearch × 9
syntax-highlighting × 8
auctex × 7
whitespace × 7
align × 7
evil × 6
major-mode × 6
latex × 5
text-editing × 5
highlighting × 5
unicode × 5
syntax-table × 5
syntax × 5
newlines × 5