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, ...
user avatar
  • 563
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 ...
user avatar
  • 43.1k
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. ...
user avatar
  • 24.2k
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
user avatar
  • 5,325
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 ...
user avatar
  • 1,240
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) ...
user avatar
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:
user avatar
  • 524
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 ...
user avatar
  • 43.1k
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
user avatar
  • 541
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 ...
user avatar
  • 506
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 ...
user avatar
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: ...
user avatar
  • 69.1k
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 ...
user avatar
  • 461
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 ...
user avatar
  • 8,536
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 ...
user avatar
  • 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 ...
user avatar
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, ...
user avatar
  • 19.1k
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 ...
user avatar
  • 43.1k
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"))) "...
user avatar
  • 8,536
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 ...
user avatar
  • 4,093
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 ...
user avatar
  • 1,490
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 ...
user avatar
  • 11.3k
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 ...
user avatar
  • 14.2k
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 ...
user avatar
  • 13.6k
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 ...
user avatar
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 ...
user avatar
  • 69.1k
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 ...
user avatar
  • 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 ...
user avatar
  • 25.2k
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 ...
user avatar
  • 19.6k

Only top scored, non community-wiki answers of a minimum length are eligible