common lisp: looking for a library which works like (str:string-case BUT with regular expessions

huangapple go评论84阅读模式
英文:

common lisp: looking for a library which works like (str:string-case BUT with regular expessions

问题

我正在尝试将一个 AWK 脚本改写成 Common Lisp(作为一种学习练习)。

我需要类似下面的代码:

  1. (case field
  2. ((: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:

  1. (str:string-case field
  2. ("FODL.*" (do something)))

(try to match the regex "FODL.*" for a field)

答案1

得分: 4

根据翻译脚本的复杂程度,你可以使用简单的cond + ppcre,或者更复杂一些的trivia模式匹配,它还包含基于ppcre的模式贡献:

  1. (ql:quickload :cl-ppcre)
  2. (ql:quickload :trivia)
  3. (ql:quickload :trivia.ppcre)
  4. (use-package '(:trivia :trivia.ppcre))
  5. (match "other"
  6. ((ppcre "som[e|a].") :some)
  7. ((ppcre "^oth") :other))
  8. ;;=> :OTHER
  9. (match "some"
  10. ((ppcre "som[e|a].") :some)
  11. ((ppcre "^oth") :other))
  12. ;;=> NIL
  13. (match "something"
  14. ((ppcre "som[e|a].") :some)
  15. ((ppcre "^oth") :other))
  16. ;;=> :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:

  1. (ql:quickload :cl-ppcre)
  2. (ql:quickload :trivia)
  3. (ql:quickload :trivia.ppcre)
  4. (use-package '(:trivia :trivia.ppcre))
  5. (match "other"
  6. ((ppcre "som[e|a].") :some)
  7. ((ppcre "^oth") :other))
  8. ;;=> :OTHER
  9. (match "some"
  10. ((ppcre "som[e|a].") :some)
  11. ((ppcre "^oth") :other))
  12. ;;=> NIL
  13. (match "something"
  14. ((ppcre "som[e|a].") :some)
  15. ((ppcre "^oth") :other))
  16. ;;=> :SOME

huangapple
  • 本文由 发表于 2023年1月8日 22:13:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定