Lisp - 将输入拆分为单独的字符串

时间:2013-03-13 18:43:19

标签: string list input split lisp

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

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

将返回:

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

请注意我的最终列表中不需要任何空格或标点符号。

非常感谢任何输入。

5 个答案:

答案 0 :(得分:19)

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))

其中delimiterp检查您是否要拆分此字符,例如

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

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

PS。查看您的预期回报值,您似乎想在my-split之前致电string-downcase

PPS。您可以轻松修改my-split以接受:start:end:delimiterp& c。

PPPS。很抱歉前两个版本my-split中的错误。请考虑一个指示器,即滚动自己的此功能版本,但使用现成的解决方案。

答案 1 :(得分:2)

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!"

答案 2 :(得分: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

答案 3 :(得分:1)

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

答案 4 :(得分:-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)))))
)
相关问题