Lisp 如果找到匹配项,则返回一行文本
如果找到匹配项,我很难确定如何返回一行文本Lisp 如果找到匹配项,则返回一行文本,lisp,newlisp,Lisp,Newlisp,如果找到匹配项,我很难确定如何返回一行文本 (set 'wireshark "http://anonsvn.wireshark.org/wireshark/trunk/manuf") (set 'arptable (map (fn (x) (parse x " ")) (exec "arp -a"))) (define (cleanIPaddress x) (slice x 1 -1)) (define (cleanMACaddress x) (upper-case (join
(set 'wireshark "http://anonsvn.wireshark.org/wireshark/trunk/manuf")
(set 'arptable (map (fn (x) (parse x " ")) (exec "arp -a")))
(define (cleanIPaddress x)
(slice x 1 -1))
(define (cleanMACaddress x)
(upper-case (join (slice (parse x ":") 0 3) ":")))
(define (addIPandMACaddress x)
(list (cleanIPaddress (nth 1 x)) (cleanMACaddress (nth 3 x))))
(set 'arplist (map addIPandMACaddress arptable))
(set 'routerMAC (last (assoc (exec "ipconfig getoption en1 router") arplist)))
(find-all routerMAC (get-url wireshark))
返回
(“20:AA:4B”)
所以我知道代码“有效”
但我想检索全文
“20:AA:4B Cisco Li#Cisco Linksys,LLC”
这可以通过使用字符串拆分
过程来执行,该过程允许我们使用删除if
(通用Lisp版本的过滤器
)通过新行分割的字符串进行搜索,删除不包含我们正在搜索的字符串的任何行。这将产生包含字符串的每一行的列表。我们将在这里定义的函数已经通过各种常见的Lisp库提供,但是出于教育目的,我们将自己定义它们。您需要的代码是这样工作的:
; First we need a function to split a string by character
(defun string-split (split-string string)
(loop with l = (length split-string)
for n = 0 then (+ pos l)
for pos = (search split-string string :start2 n)
if pos collect (subseq string n pos)
else collect (subseq string n)
while pos))
; Now we will make a function based on string-split to split by newlines
(defun newline-split (string)
(string-split "
" string))
; Finally, we go through our text searching for lines that match our string.
; Make sure to replace 'needle' with the string you wish to search for.
(remove-if #'(lambda (x)
(equal 'nil (search (string-upcase "needle")
(string-upcase x))))
(newline-split haystack))
您应该能够对发布的代码应用此策略,只需进行一些小的修改。该代码在Mac OS X 10.7.5上的SBCL 1.0.55.0-abb03f9(ANSI Common Lisp的实现)上进行了测试。最后我使用了:
(find-all (string routerMAC ".*") (get-url wireshark))
Wireshark项目于2014年1月从Subversion转向Git。“manuf”URL现在是