Created
January 24, 2020 05:48
-
-
Save Liutos/045aa43b5b3698d7e7c460d3f0c90858 to your computer and use it in GitHub Desktop.
提取字符串中所有匹配正则表达式的第一个register的内容
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
(ql:quickload 'cl-ppcre) | |
(defun all-first-registers-to-strings (regex target-string) | |
"返回TARGET-STRING中所有匹配正则表达式REGEX的字符串属于第一个register的内容。" | |
(check-type regex string) | |
(check-type target-string string) | |
(let ((pos 0) | |
(strs '())) | |
(loop | |
(multiple-value-bind (start end register-begins register-ends) | |
(cl-ppcre:scan regex target-string :start pos) | |
(unless start | |
(return-from all-first-registers-to-strings (nreverse strs))) | |
(push (subseq target-string (svref register-begins 0) (svref register-ends 0)) strs) | |
(setf pos end))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment