Skip to content

Instantly share code, notes, and snippets.

@benhsu
Created January 17, 2013 04:33
Show Gist options
  • Save benhsu/4553672 to your computer and use it in GitHub Desktop.
Save benhsu/4553672 to your computer and use it in GitHub Desktop.
code to grab all the strings matching a given regex and put them in a buffer, along with sample use for IPs
(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]+"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment