如何在 Common Lisp 中使用多字符分隔符将字符串拆分为子字符串?

问题描述 投票:0回答:1

使用 split-sequence:split-sequence 将字符串拆分为由单个字符分隔的子字符串显然非常容易。在我看来,将字符串拆分为由多字符字符串分隔的子字符串应该同样容易。我正在编写一个函数来执行此操作(尚未找到内置/库),但我意识到也许我遗漏了一些东西,并且想知道您是否有指针。

我正在尝试做的事情的例子:

(special-split-sequence ". " "Hello. Dolly") ; -> ("Hello" "Dolly")
(special-split-sequence "xyz" "abcxyzabc") ; -> ("abc" "abc")

想法?

lisp common-lisp sbcl
1个回答
0
投票

您可以使用

cl-ppcre
str
str:split
来实现这一点。

(str:split ". " "Hello. Dolly") ;; -> ("Hello" "Dolly")
(str:split "xyz" "abcxyzabc") ;; -> ("abc" "abc")

它在底层使用

cl-ppcre
,但它负责 not 默认将分隔符字符串视为正则表达式。它的实现是这样的:

(defun split (separator s &key (omit-nulls *omit-nulls*) limit (start 0) end regex)
  "Split s into substring by separator (cl-ppcre takes a regex, we do not).

   `limit' limits the number of elements returned (i.e. the string is
   split at most `limit' - 1 times).
   If `regex' is not nil, `separator' is treated as a regular expression.

   Examples:
   (str:split \",\" \"foo,bar\") ;; => (\"foo\" \"bar\")
   (str:split \"[,|;]\" \"foo,bar;baz\" :regex t) ;; => (\"foo\" \"bar\" \"baz\")
"
  ;; cl-ppcre:split doesn't return a null string if the separator appears at the end of s.
  (let* ((limit (or limit (1+ (length s))))
         (res (if regex
                  (ppcre:split separator s :limit limit :start start :end end)
                  (ppcre:split `(:sequence ,(string separator)) s :limit limit :start start :end end))))
    (if omit-nulls
        (remove-if (lambda (it) (emptyp it)) res)
        res)))

它的其他关键参数是

:omit-nulls
:limit
:start
:end
:regex

您对食谱有提示:https://lispcookbook.github.io/cl-cookbook/strings.html

(我是这个lib的原作者)

© www.soinside.com 2019 - 2024. All rights reserved.