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

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

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

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:

确定