19

我试图接受用户输入并将其存储在一个列表中,而不是一个由单个字符串组成的列表,我希望扫描的每个单词都是它自己的字符串。例子:

> (input)
This is my input. Hopefully this works

会返回:

("this" "is" "my" "input" "hopefully" "this" "works")

请注意,我不希望在最终列表中出现任何空格或标点符号。

任何投入将不胜感激。

4

5 回答 5

21

split-sequence是现成的解决方案。

你也可以自己动手:

(defun my-split (string &key (delimiterp #'delimiterp))
  (loop :for beg = (position-if-not delimiterp string)
    :then (position-if-not delimiterp string :start (1+ end))
    :for end = (and beg (position-if delimiterp string :start beg))
    :when beg :collect (subseq string beg end)
    :while end))

wheredelimiterp检查您是否要拆分此字符,例如

(defun delimiterp (c) (or (char= c #\Space) (char= c #\,)))

或者

(defun delimiterp (c) (position c " ,.;/"))

PS。查看您的预期返回值,您似乎想调用string-downcasebefore my-split

聚苯乙烯。您可以轻松修改my-split为接受:start, :end, :delimiterp&c。

购买力平价。很抱歉前两个版本中的错误my-split。请考虑一个指标,即不应推出自己的此功能版本,而应使用现成的解决方案。

于 2013-03-13T18:58:05.837 回答
7

对于 Common-Lisp 中的该任务,我发现它很有用(uiop:split-string str :separator " "),并且该包uiop通常有很多实用程序,请查看文档https://common-lisp.net/project/asdf/uiop.html#index-split_002dstring .

于 2019-01-14T13:12:01.920 回答
5

cl-ppcre:split

* (split "\\s+" "foo   bar baz
frob")
("foo" "bar" "baz" "frob")

* (split "\\s*" "foo bar   baz")
("f" "o" "o" "b" "a" "r" "b" "a" "z")

* (split "(\\s+)" "foo bar   baz")
("foo" "bar" "baz")

* (split "(\\s+)" "foo bar   baz" :with-registers-p t)
("foo" " " "bar" "   " "baz")

* (split "(\\s)(\\s*)" "foo bar   baz" :with-registers-p t)
("foo" " " "" "bar" " " "  " "baz")

* (split "(,)|(;)" "foo,bar;baz" :with-registers-p t)
("foo" "," NIL "bar" NIL ";" "baz")

* (split "(,)|(;)" "foo,bar;baz" :with-registers-p t :omit-unmatched-p t)
("foo" "," "bar" ";" "baz")

* (split ":" "a:b:c:d:e:f:g::")
("a" "b" "c" "d" "e" "f" "g")

* (split ":" "a:b:c:d:e:f:g::" :limit 1)
("a:b:c:d:e:f:g::")

* (split ":" "a:b:c:d:e:f:g::" :limit 2)
("a" "b:c:d:e:f:g::")

* (split ":" "a:b:c:d:e:f:g::" :limit 3)
("a" "b" "c:d:e:f:g::")

* (split ":" "a:b:c:d:e:f:g::" :limit 1000)
("a" "b" "c" "d" "e" "f" "g" "" "")

http://weitz.de/cl-ppcre/#split

对于常见情况,有(新的,“现代且一致的”)cl-str字符串操作库:

(str:words "a sentence    with   spaces") ; cut with spaces, returns words
(str:replace-all "," "sentence") ; to easily replace characters, and not treat them as regexps (cl-ppcr treats them as regexps)

你有cl-slug来删除非 ascii 字符和标点符号:

 (asciify "Eu André!") ; => "Eu Andre!"

以及str:remove-punctuation(使用cl-change-case:no-case)。

于 2017-01-03T01:35:14.283 回答
1
; in AutoLisp usage (splitStr "get off of my cloud" " ") returns (get off of my cloud)

(defun splitStr (src delim / word letter)

  (setq wordlist (list))
  (setq cnt 1)
  (while (<= cnt (strlen src))

    (setq word "")

    (setq letter (substr src cnt 1))
    (while (and (/= letter delim) (<= cnt (strlen src)) ) ; endless loop if hits NUL
      (setq word (strcat word letter))
      (setq cnt (+ cnt 1))      
      (setq letter (substr src cnt 1))
    ) ; while

    (setq cnt (+ cnt 1))
    (setq wordlist (append wordlist (list word)))

  )

  (princ wordlist)

  (princ)

) ;defun
于 2014-07-25T19:51:03.693 回答
-1
(defun splitStr (src pat /)
    (setq wordlist (list))
    (setq len (strlen pat))
    (setq cnt 0)
    (setq letter cnt)
    (while (setq cnt (vl-string-search pat src letter))
        (setq word (substr src (1+ letter) (- cnt letter)))
        (setq letter (+ cnt len))
        (setq wordlist (append wordlist (list word)))
    )
    (setq wordlist (append wordlist (list (substr src (1+ letter)))))
)
于 2017-06-08T14:44:22.773 回答