12

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 learn and train regular expressions interactively in Emacs, similarly to the built-in Emacs tutorial? If not, what are other suggestions to utilize Emacs to practice the use of regular expressions for searching and replacing?

17

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

visual-regexp snapshot

  • 3
    Or even anzu, which does the same and more. – sanityinc Oct 8 '14 at 16:20
16

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

  • 7
    Don't panic when C-g doesn't exit like you expect. C-c C-q is the default keybinding to quit regexp builder when you are done practicing. – nispio Oct 8 '14 at 13:15
5

I use regexp-builder which let's you test regexps interactively and get feedback. And of course, once I started using that, I immediately asked myself "How do I feed this regexp into query-replace-regexp?"

The answer I chose is the following function defined on the EmacsWiki ReBuilder page.

(defun reb-query-replace (to-string)
  "Replace current RE from point with `query-replace-regexp'."
  (interactive
   (progn (barf-if-buffer-read-only)
          (list (query-replace-read-to (reb-target-binding reb-regexp)
                                       "Query replace"  t))))
  (with-current-buffer reb-target-buffer
    (query-replace-regexp (reb-target-binding reb-regexp) to-string)))

So my workflow is:

  1. M-x re-builder
  2. type type type
  3. M-x reb-query-replace
3

It can be a tremendous help when learning to use regexps to see what is matched by which parts of a regexp - in particular, regexp groups. And to do that interactively and incrementally.

Along the lines of @immerr's answer, Icicles search has had similar highlighting for a long time.

enter image description here

3

You can also use helm-regexp that displays regex groups separately.

1

Not within emacs, but http://rubular.com/ is basically regexp-builder with a cheat-sheet. You can set up an example and have it spit out a link to that example. I use this when discussing regex with a team.

  • 1
    Does Ruby use emacs-flavored Regexp? – nispio Oct 8 '14 at 13:09
1

As an alternative to Dave F's regexp-builder you can also create arbitrary text to practice pattern matching against.

The most useful feature of this command is that it will highlight matches in the buffer as the pattern is created to assist you in ensuring the proper pattern matches.

Using (query-replace-regexp) (C-M-%) and creating a regular expression to perform the replacement you want will help you practice.

  • What benefit does this offer to someone wanting to "practice" regular expressions that regexp-builder does not already offer? The problem with query-replace-regexp is that you need to write an entire regexp and then commit to it to find out what it matches. If you don't like the result you have to start all over again. regexp-builder shows matches on the fly which makes it easier to tweak your regexp until it works if you don't know what you are doing. – nispio Oct 8 '14 at 13:16
  • 1
    I admit that's the downside of it. However if you're familiar with regular expressions using query-replace-regexp will get you used to using it in Emacs specifically – Jonathan Leech-Pepin Oct 8 '14 at 13:38
  • Perhaps... but it doesn't seem to match what the OP is asking for. "If you're familiar with regular expressions," then you don't really have a need to learn regular expressions at all. – nispio Oct 8 '14 at 13:42
  • 1
    OP is asking how to learn how/when to use them interactively as an Emacs beginner. They might already have some regexp knowledge from other types of regexp. – Jonathan Leech-Pepin Oct 8 '14 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.