英文:
common lisp: looking for a library which works like (str:string-case BUT with regular expessions
问题
我正在尝试将一个 AWK 脚本改写成 Common Lisp(作为一种学习练习)。
我需要类似下面的代码:
(case field
  ((:regex "FODL.*") (do-something)))
(尝试匹配字段的正则表达式 "FODL.*")
英文:
I am trying to rewrite an AWK script in Common Lisp (as a kind of learning exercise)
I need something like:
(str:string-case field
  ("FODL.*" (do something)))
(try to match the regex "FODL.*" for a field)
答案1
得分: 4
根据翻译脚本的复杂程度,你可以使用简单的cond + ppcre,或者更复杂一些的trivia模式匹配,它还包含基于ppcre的模式贡献:
(ql:quickload :cl-ppcre)
(ql:quickload :trivia)
(ql:quickload :trivia.ppcre)
(use-package '(:trivia :trivia.ppcre))
(match "other"
  ((ppcre "som[e|a].") :some)
  ((ppcre "^oth") :other))
;;=> :OTHER
(match "some"
  ((ppcre "som[e|a].") :some)
  ((ppcre "^oth") :other))
;;=> NIL
(match "something"
  ((ppcre "som[e|a].") :some)
  ((ppcre "^oth") :other))
;;=> :SOME
以上是翻译好的内容。
英文:
depending on the translated script complexity, you can use something as simple as cond + ppcre, or something more sophisticated, like trivia pattern matching, which also has ppcre based patterns contrib:
(ql:quickload :cl-ppcre)
(ql:quickload :trivia)
(ql:quickload :trivia.ppcre)
(use-package '(:trivia :trivia.ppcre))
(match "other"
  ((ppcre "som[e|a].") :some)
  ((ppcre "^oth") :other))
;;=> :OTHER
(match "some"
  ((ppcre "som[e|a].") :some)
  ((ppcre "^oth") :other))
;;=> NIL
(match "something"
  ((ppcre "som[e|a].") :some)
  ((ppcre "^oth") :other))
;;=> :SOME
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论