Created
January 17, 2013 04:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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