【问题标题】:CL-PPCRE Unicode PropertyCL-PPCRE Unicode 属性
【发布时间】:2015-03-25 14:41:46
【问题描述】:

我正在尝试使用 CL-PPCRE 库为这个简单的 Perl 代码找到解决方案:

if (/\p{Space}/){
  print "This string has some spaces\n";
}

我是 CL-PPCRE 的新手并尝试过:

   (scan "\\p{\\#Space}" "The String has some white spaces")

;我收到一条错误消息,提示属性 #/Space 不存在。

如何执行等价的?

【问题讨论】:

    标签: common-lisp cl-ppcre


    【解决方案1】:

    perl 正则表达式/\p{Space}/ 不仅仅匹配“”。 cf\p{} docs

    一种方法是只使用\s 表达式:

    (cl-ppcre:scan "\\s" (format nil "hi~Cthere" #\return))
    

    要使用整个 unicode Space 类:

    (ql:quickload :cl-unicode)
    (cl-ppcre:scan "\\p{Space}" (format nil "hi~Cthere" #\return))
    

    请参阅 CL-PPCRE 文档中的 Unicode properties

    【讨论】:

    • 我试过你的代码,它总是产生 NIL:(cl-ppcre:scan "\\p{Space}" (format t "hi~Cthere" #\return))
    • 我尝试了以下方法,但它总是产生 NIL: (if(cl-ppcre:scan "\\p{Space}" "There is a space here.") (print "Success!" )) ;=>NIL if (cl-ppcre:scan "\\p{Digit}" "有一个数字 411")(print "Success!") ;=>nil. {whatever} 似乎不起作用?
    • note (format t ...) 打印到*standard-output*,(format nil ...) 写入一个新字符串。
    【解决方案2】:

    cl-ppcre 库不要求您(至少在空间方面)使用任何特殊常量。

    (if (cl-ppcre:scan " " "alle meine entchen")
        (FORMAT T "Does have spaces~%")
        (FORMAT T "Does not have spaces~%"))
    > Does have spaces
    
    (if (cl-ppcre:scan " " "allemeineentchen")
        (FORMAT T "Does have spaces~%")
        (FORMAT T "Does not have spaces~%"))
    > Does not have spaces
    

    会成功的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多