使用 CL-PPCRE 绑​​定寄存器组

CL-PPCRE:REGISTER-GROUPS-BIND 将匹配正则表达式的字符串,如果匹配,则将正则表达式中的寄存器组绑定到变量。如果字符串不匹配,则返回 NIL

(defun parse-date-string (date-string)
  (cl-ppcre:register-groups-bind
      (year month day)
      ("(\\d{4})-(\\d{2})-(\\d{2})" date-string)
    (list year month day)))

(parse-date-string "2016-07-23") ;=> ("2016" "07" "23")
(parse-date-string "foobar")     ;=> NIL
(parse-date-string "2016-7-23")  ;=> NIL