Skip to content

Instantly share code, notes, and snippets.

@redraiment
Created October 10, 2013 16:20
Show Gist options
  • Save redraiment/6921169 to your computer and use it in GitHub Desktop.
Save redraiment/6921169 to your computer and use it in GitHub Desktop.
Emacs Lisp 正则表达式简化
(defun conj (v &rest c)
(append v c))
(defun r (exp)
(do ((e (cons nil (string-to-list exp))
(let ((s (car e)) (x (cdr e))
(a (cadr e)) (d (caddr e)))
(cond
((= a ?%)
(cons (apply (apply-partially #'conj s)
(conj (if (memq d (string-to-list "[wbsc]"))
(list ?\\))
d))
(nthcdr 2 x)))
((memq a (string-to-list "(){}|"))
(cons (conj s ?\\ a) (cdr x)))
(t (cons (conj s a) (cdr x)))))))
((null (cdr e)) (apply #'string (car e)))))
@redraiment
Copy link
Author

(r "(a|b){2,3}%%%(x%)%{%} %w %b %s- %cl abcd%%")
; => "(a|b){2,3}%(x){} \w \b \s- \cl abcd%"
还需要考虑[]中的特殊字符将变成普通字符

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment