【问题标题】:Using regex (Regular Expressions) in Parenscript在 Parenscript 中使用正则表达式(正则表达式)
【发布时间】:2020-09-14 10:18:55
【问题描述】:

我正在尝试 Parenscript。在尝试使用正则表达式函数时,我得到了意外的输出。例如,reference manual 显示:

(regex "foobar")
    /foobar/;

(regex "/foobar/i")
    /foobar/i; 

但是,在我的 repl 中,我收到函数 (parenscript:regex..) 未定义的错误。

The function parenscript:regex is undefined.
   [Condition of type undefined-function]

Restarts:
 0: [continue] Retry using regex.
 1: [use-value] Use specified function
 2: [retry] Retry SLIME REPL evaluation request.
 3: [*abort] Return to SLIME's top level.
 4: [abort] abort thread (#<thread "repl-thread" running {1002319B63}>)

Backtrace:
  0: (sb-impl::retry-%coerce-name-to-fun regex nil)
  1: (sb-int:simple-eval-in-lexenv (regex "foobar") #<NULL-LEXENV>)
  2: (eval (regex "foobar"))
 --more--

我尝试将命名空间更改为 cl-user、ps 等,但没有任何效果。 OTOH,只是为了检查,我尝试使用函数 sin、random 等,并且有效。我的意思是,在 repl 中输入 (sin 0.1) 会产生预期的输出。

任何帮助都会有很大帮助。

【问题讨论】:

    标签: regex lisp common-lisp parenscript


    【解决方案1】:

    在 SBCL 读取评估打印循环中:

    CL-USER> (in-package "PS")
    #<PACKAGE "PARENSCRIPT">
    
    PS> (ps (regex "foobar"))
    "/foobar/;"
    
    PS> (in-package "CL-USER")
    #<package "COMMON-LISP-USER">
    
    cl-user> (ps:ps (ps:regex "foobar"))
    "/foobar/;"
    

    PS:PS 是 Parenscript 编译器作为宏运算符。

    【讨论】:

    • 非常感谢Rainer Joswig。我错过了第二个括号内的 ps:regex 。我还有另一个问题。如果我在 PS> 中执行 (random 30),它会产生一个低于 30 的随机整数。但是 (ps:ps (ps:random 30)) 会产生 JavaScript 代码,而不是随机数。有没有办法理解潜在的一致性/主题?我将尝试将它用于 html 表单中的表单验证。再次感谢您。
    • 我认为我的评论令人费解。试图简化。在 PS> (random 30) -> integer below 30. (regex "foobar") 中抛出错误。 (ps (regex "foobar")) -> "/foobar/;"在 cl-user> (ps:ps (ps:regex "foobar")) -> "/foobar/;"。 (ps:ps (ps:random 30)) -> javascript 代码,不是 30 以下的整数。有没有办法理解会发生什么?正弦函数像随机函数一样工作。仅在 PS> 中给出数值结果,在 cl-user> 中给出 js-code。正弦的行为类似于随机。正则表达式似乎表现不同。
    • 此外,您能否通过示例类似地展示如何使用带有 cl-interpol 的正则表达式?在执行 (ql:quickload :cl-interpol) 并进入 PS> 之后,我执行了 (ps (regex #?r"/([^\s]+)foobar/i")) 并引发错误。没有为 #\ 定义调度函数?先感谢您。并且很抱歉用这么多问题纠缠你。
    • 你了解 Parenscript 是什么以及 PS:PS 是做什么的吗?
    • 根据我的理解(如果我遗漏了什么,请纠正我):Parenscript 将类似 CL 的语句(一个子集,以及一些浏览器命令(如窗口、文档等)的添加)转换为 javascript。在“PS”包中,我们可以使用普通的 ps,而从外部(如果我们没有导入/使用它)我们需要 ps:ps。所以我的困惑是/是为什么在 PS> 命名空间 (random 30) 和 (sin 0.1) 内评估值,但 (regex ...) 不是。虽然 (ps (regex ..))、(ps (random ..)) 和 (ps (sin ..)) 等正在(正确地)转换为 javascript。
    猜你喜欢
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多