Skip to content

Instantly share code, notes, and snippets.

@codeforkjeff
Created April 12, 2013 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeforkjeff/5369015 to your computer and use it in GitHub Desktop.
Save codeforkjeff/5369015 to your computer and use it in GitHub Desktop.
(defun my-replace (regex rep str &optional start)
(save-match-data
(let ((found-pos (string-match regex str (or start 0))))
(if found-pos
(let* ((match (substring str found-pos (match-end 0)))
(replacement (if (stringp rep)
rep
(or (funcall rep match) ""))))
(my-replace regex
rep
(concat (substring str 0 found-pos)
replacement
(substring str (+ found-pos (length match))))
(+ found-pos (length replacement))))
;; else, we're done
str))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment