public
Created

code to grab all the strings matching a given regex and put them in a buffer, along with sample use for IPs

  • Download Gist
collect-ips.el
Emacs Lisp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
(defun collect-regexp-results (regex)
;;; collects all the matches of regex in a buffer called *collect-result*
;;; then switches to that buffer
;;; TODO refactor this to take the region as a parameter
(interactive "Mregex to search for: ")
(let ((curmin (region-or-buffer-beginning))
(curmax (region-or-buffer-end)))
(save-excursion
(goto-char curmin)
;; (goto-char (region-or-buffer-beginning))
(while (re-search-forward regex curmax t)
(let ((retval (match-string-no-properties 0)))
(with-current-buffer (get-buffer-create "*collect results*")
(insert retval)
(insert "\n"))))
(switch-to-buffer "*collect results*"))))
 
(defun collect-ip-addresses ()
(interactive)
(collect-regexp-results "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.