2017-07-03 88 views

回答

1

下你想要做什麼:

(defun parse-integer-list (str) 
    "Parse string representing a range of integers into a list of integers." 
    (let (start ranges) 
    (while (string-match "\\([0-9]+\\)\\(?:-\\([0-9]+\\)\\)?" str start) 
     (push 
     (apply 'number-sequence 
       (seq-map 'string-to-int 
         (seq-filter 
         'identity 
         (list (match-string 1 str) (match-string 2 str))))) 
      ranges) 
     (setq start (match-end 0))) 
    (nreverse (seq-mapcat 'nreverse ranges)))) 

代碼遍歷輸入字符串搜索爲純數字或數字的範圍。在每場比賽中,它都會調用number-sequence,其中只有一個簡單匹配的數字或一個範圍匹配的兩個數字,並將每個結果的數字序列推入列表中。爲了解決push向後生成結果,最後它將列表中的所有範圍顛倒過來,連接它們,然後反轉結果並返回。

調用parse-integer-list與例如輸入:

(parse-integer-list "1-3, 4, 8, 18-21") 

生產:

(1 2 3 4 8 18 19 20 21)