-
-
Save redraiment/6921169 to your computer and use it in GitHub Desktop.
Emacs Lisp 正则表达式简化
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 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))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(r "(a|b){2,3}%%%(x%)%{%} %w %b %s- %cl abcd%%")
; => "(a|b){2,3}%(x){} \w \b \s- \cl abcd%"
还需要考虑[]中的特殊字符将变成普通字符