![[Home]](https://www.emacswiki.org/images/logo218x38.png)
This is a nice interactive tool for building regular expressions in emacs. From the commentary:
;; When I have to come up with regular expressions that are more ;; complex than simple string matchers, especially if they contain sub ;; expressions, I find myself spending quite some time in the ;; `development cycle'. `re-builder' aims to shorten this time span so ;; I can get on with the more interesting bits. ;; With it you can have immediate visual feedback about how well the ;; regexp behaves to your expectations on the intended data.
Re-builder is included in GNU Emacs 21, you can download it from CVS here:
re-builder highlights both the matched expressions and the expressions to be saved into registers.
re-builder is also available as easily installable XEmacs Package.
I just managed to add perl regexp support to re-builder, of which there will be a re-builder XEmacs pre-release package soon. – AdrianAichner
In GnuEmacs 21.4, it needs slightly modifications.
--- /home/u/downloads/re-builder.el 2006-03-09 23:38:14.000000000 +0900 +++ /home/u/emacs/lisp/re-builder.el 2006-03-09 23:19:17.000000000 +0900 @@ -873,13 +873,16 @@ (if (reb-target-binding case-fold-search) "i" "") ;; use extended perl regexp syntax? "x") - (buffer-string reb-target-buffer))) - (call-process "perl" - (list (get-buffer " *reb-perl-program*")) - match-buffer)) + (with-current-buffer reb-target-buffer (buffer-string)))) + (let ((tmpfile "/tmp/.reb")) + (write-region 1 (point-max) tmpfile nil 'nodisp) + (call-process "perl" + tmpfile + ;;(list (get-buffer " *reb-perl-program*")) + match-buffer))) (with-current-buffer reb-target-buffer (setq reb-perl-match-vector - (read (buffer-string match-buffer))))) + (read (with-current-buffer match-buffer (buffer-string )))))) (kill-buffer program-buffer) (kill-buffer match-buffer)))) (let* ((vector (reb-target-binding reb-perl-match-vector)) @@ -911,7 +914,7 @@ nil)) ;; index to end of subexp 0 (end of whole match): 1)))))) - (t (re-search-forward regexp limit noerror count buffer)))) + (t (re-search-forward regexp limit noerror count )))) (defun reb-re-search-backward (regexp &optional limit noerror count buffer) (cond @@ -944,7 +947,7 @@ nil)) ;; index to end of subexp 0 (end of whole match): 1)))))) - (t (re-search-backward regexp limit noerror count buffer)))) + (t (re-search-backward regexp limit noerror count )))) (provide 're-builder)
Lisp:re-builder-from-xemacs-patched.el
Alternate Perl support
At
http://cpansearch.perl.org/src/YEWENBIN/Emacs-PDE-0.2.16/lisp/re-builder-x.el
you can find an alternate implimentation for perl regexes. It works better for me than the other versions (The other version I have found won’t match a space character, oddly).
The command, ‘C-c C-w’ can yank the current regular expression to the KillRing. As of Emacs 23, it does so as a string, reglardless of the current syntax you are using to enter the regular expression. This is suited for Emacs Lisp programming. It is not helpful for those who use RE-Builder for building lots of complex searches with grouping and then want to prepare it for ‘query-replace-regexp’. One solution is to use this command:
(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)))
[new]
[new] When you use reb-query-replace it starts from the current position of point in the target buffer. Here are a couple of functions to change that position:
(defun reb-beginning-of-buffer () "In re-builder, move target buffer point position back to beginning." (interactive) (set-window-point (get-buffer-window reb-target-buffer) (with-current-buffer reb-target-buffer (point-min)))) (defun reb-end-of-buffer () "In re-builder, move target buffer point position back to beginning." (interactive) (set-window-point (get-buffer-window reb-target-buffer) (with-current-buffer reb-target-buffer (point-max))))
Another little trick that I find useful is to use Yasnippet to store useful but hard to remember regexp’s. If you put your snippets in a folder names “reb-mode” under the “snippets” directory then they will be available to you in re-builder. – JoeBloggs