common lisp 的正则库 cl-ppcre : http://weitz.de/cl-ppcre/

提取括号匹配内容到列表

* (scan-to-strings "(([^b])*)b" "aaabd")
"aaab"
#("aaa" "a")

提取括号匹配内容到指定变量

* (register-groups-bind (fname lname (#'parse-integer date month year))
      ("(\\w+)\\s+(\\w+)\\s+(\\d{1,2})\\.(\\d{1,2})\\.(\\d{4})" "Frank Zappa 21.12.1940")
    (list fname lname (encode-universal-time 0 0 0 date month year 0)))
("Frank" "Zappa" 1292889600) 

提取匹配内容到指定变量

* (all-matches-as-strings "\\w*" "foo bar baz")
("foo" "" "bar" "" "baz" "")

分割字符串

* (split "(\\s+)" "foo bar   baz")
("foo" "bar" "baz")

替换

* (regex-replace "(?i)fo+" "FOO bar" "frob")
"frob bar"
T

全部替换

* (regex-replace-all "(?i)f(o+)" "foo Fooo FOOOO bar" "fr\\1b" :preserve-case t)
"froob Frooob FROOOOB bar" 

字符串反引用,相当于perl里的quotemeta

* (quote-meta-chars "[a-z]*")
"\\[a\\-z\\]\\*"


blog comments powered by Disqus

Published

07 June 2013

Tags