Skip to content

Instantly share code, notes, and snippets.

@tsleyson
Last active September 14, 2017 23:46
Show Gist options
  • Save tsleyson/4ea046f87f07602db0d9907a4833213f to your computer and use it in GitHub Desktop.
Save tsleyson/4ea046f87f07602db0d9907a4833213f to your computer and use it in GitHub Desktop.
Emacs functions for searching for a regex in a region and dumping the results in a temp buffer
;; This function stolen from https://emacs.stackexchange.com/a/7150/12219
(defun re-seq (regexp string)
"Get a list of all regexp matches in a string"
(save-match-data
(let ((pos 0)
matches)
(while (string-match regexp string pos)
(push (match-string 0 string) matches)
(setq pos (match-end 0)))
matches)))
(require 'cl)
(defun re-seq-in-region-unique (start end regex)
"Select a region and enter a regex to search for. Results will be dumped in a temp buffer."
(interactive "r \nsRegex: ")
(with-output-to-temp-buffer (make-temp-name "re-seq-results")
(let ((matches (remove-duplicates
(re-seq regex (buffer-substring-no-properties start end))
:test (lambda (x y) (or (null y) (equal x y))))))
(while matches
(princ (concat (car matches) "\n"))
(setq matches (cdr matches))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment