31

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, 1, and y. How it works: The regexp is tried on every line in the region. On each, if the lines are not aligned already, it will match on a different column. ...


29

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 comprehensive and almost entirely syntax-compatible, you can really ignore sregex unless you happen to be working with old code that used it). The other two syntax ...


18

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 form, but a list of regexp forms. Since what you mean is the sequence of regular expressions symbol-start followed by one of a bunch of strings followed by symbol-...


17

Besides regexp-builder you might also consider visual-regexp to provide you with visual feedback on the replace in progress:


17

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. This expression literally searches for a newline: C-j.


16

You can try M-x regexp-builder RET which is an interactive regular expression matcher, that's not bad


16

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) (while (string-match regexp string pos) (push (match-string 0 string) matches) (setq pos (match-end 0))) matches))) ; Sample URL (setq ...


15

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 emacs:


14

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: When matching a string instead of a buffer, `$' matches at the end of the string or before a newline character. Similarly, \` matches the beginning of the ...


13

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, to get an idea of how rx works: This ^;;\\s-*\n\\|^\n becomes (rx (or (and line-start ";;" (0+ space) ?\n) (and line-start ?\n))) This [ \t\n]*:\\([^:]...


13

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 just use a newline, exactly like any other literal character. If you are entering a regexp interactively then you can insert the newline with C-qC-j, as ...


13

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"))) "[A-Za-z]+" That doesn't answer your question directly; it pushes the question to "are these two regexes identical?" So, let's look for an uppercase or ...


12

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 expression used as part of the replacement text. Inside of that expression, ‘\&’ is a string denoting the whole match, ‘\N’ a partial match, ‘#&’ and ‘...


12

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 REGEXP. When called from Lisp (and usually when called interactively as well, see below), applies to the part of the buffer after point. The line point is ...


11

...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


11

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 buffer (or its accessible portion) being matched against.


11

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 words (separated by spaces), or a regexp (using some regexp special characters). If it is a word, search for matches for that word as a substring. If it is a ...


10

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 makes it a group. That means that for example, \([[:digit:]]+\)? would match either 123 or some empty string, and that you can use \1 (assuming it is your only ...


10

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 Emacs Lisp language implementation.


9

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 Manual for more.


9

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 Emacs bug #17284 for an example.


9

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 buffer are string; using it in a search function will succeed if the text being searched contains string. (regexp-quote "^The cat$") => \\^The cat\\$


9

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 in the buffer are string; using it in a search function will succeed if the text being searched contains string. This allows you to request an exact string ...


9

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 the stack, .*\n should be handled very efficiently, i.e. without backtracking and without eating up the stack. On the contrary [\0-\377[:nonascii:]]*\n is ...


9

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 syntax table; in programming language modes it's meant to be what the language treats as a symbol or identifier. You can also use \<Vector\> or \bVector\...


9

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 since Emacs 24.4: (eval-when-compile (require 'subr-x)) (string-trim "\n\r\s\tfoo\n\r\s\t") ; => "foo" Since Emacs 26.1 these inline functions also accept ...


8

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 foo with bar: M-% foo RET bar RET ! This makes use of the fact that query-replace will work on the active region instead of the whole buffer if there is one. ...


8

First, you need to be sure that the buffer is in Text mode: M-x text-mode Once in text mode, C-- M-x align (which invokes the text-dollar-figure alignment rule) will align the first decimal vertically. But it fails to align the second decimal column. Plus it fails to align numbers that don't have an explicit decimal point (such as wolfram-mathematica's ...


8

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 with a capture group, this allows one to extract whatever pattern is desired. For example, C-u M-x occur followed by \"\(.*\)\" will prompt the user for which ...


8

I don't know where your expectation comes from, but I for my part don't know any programming language where it'd hold. In every regular expression engine I'm aware of anonymous groups are numbered in order of appearance, Emacs Lisp being no exception. Your pattern has two distinct anonymous groups, which are numbered from left to right. Hence the first ...


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