Elisp: Tip: How to Test Regex

By Xah Lee. Date: .

One simple way to test regex is to create a file with the following content:

(re-search-forward "yourRegex")

big text to search here


(let ((case-fold-search nil))
  (re-search-forward "\\.jpg"))
;; cat.jpg

save the file as test.el, reopen it, so it opens in emacs lisp mode so you have syntax coloring.

Then, put your cursor to the right of the closing parenthesis, then Alt+x eval-last-sexp. If your regex matches, it'll move cursor to the last char of the matched text. If you get a lisp error saying search failed, then your regex didn't match. If you get a lisp syntax error, then you probably screwed up on the backslashs.

Elisp, Regex in Lisp Code