38 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 ...
  • 44.2k
37 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, ...
  • 583
31 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.5k
25 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
  • 5,423
23 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 ...
  • 1,270
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:
  • 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 ...
  • 44.2k
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
  • 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 ...
  • 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 ...
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: ...
  • 70.9k
16 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 ...
  • 8,639
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 ...
  • 461
14 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 ...
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 ...
  • 31.3k
14 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 ...
  • 44.2k
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, ...
  • 19.5k
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"))) "...
  • 8,639
13 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 ...
  • 11.5k
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 ...
  • 4,133
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 ...
  • 1,490
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 ...
  • 14.3k
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 ...
  • 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 ...
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 ...
  • 70.9k
10 votes

Is there an equivalent of string-match for regexp?

For the sake of an answer: string-match is meant for to be used with regexps. (string-match REGEXP STRING &optional START) Return index of start of first match for REGEXP in STRING, or ...
10 votes
Accepted

How to apply arithmetic operators to query-replace-regex

\2 in your replacement is a string, and it needs to be a number in order to perform the division. You could convert it to a number using string-to-number, but there's in-built shorthand for treating ...
  • 4,167
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 ...
  • 31.3k

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