Created
April 12, 2013 03:08
-
-
Save codeforkjeff/5369015 to your computer and use it in GitHub Desktop.
string-match code fragment #5
see http://codefork.com/blog/index.php/2013/04/11/string-match-and-dealing-with-state/
This file contains hidden or 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 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