Paste number 40088: irc parse-message with regex

Index of paste annotations: 1

Paste number 40088: irc parse-message with regex
Pasted by: Cin
When:2 years, 2 months ago
Share:Tweet this! | http://paste.lisp.org/+UXK
Channel:#lisp
Paste contents:
Raw Source | XML | Display As
(defmacro array-value-bind (variables array &body body)
  (let ((array-sym (gensym)))
    `(let ((,array-sym ,array))
       (let (,@(loop for index from 0 to (length variables)
		     for variable in variables
		     collect `(,variable (aref ,array-sym ,index))))
	 ,@body))))

(defun parse-message (msg)
  (let* ((letter "a-zA-Z")
	 (digit "0-9")
	 (special "\\[|\\]|\\\\|`|^|\\{|\\}|_")
	 (nickname (format nil "([~A|~A][~A|~A|~A|-]*)" letter special letter digit special))
	 (host "([a-zA-Z\\/]*)")
	 (prefix (format nil "~A(?:(?:\\!([a-z]*))?\\@~A)?" nickname host))
	 (command "([A-Z]*)")
	 (message (format nil "(?:\\:~A[ ])?(?:~A)([ ].*)" prefix command)))
    (multiple-value-bind (string components)
	(cl-ppcre:scan-to-strings message msg)
	(declare (ignore string))
	(array-value-bind (nickname user host command params)
			  components
			  (format t "User: ~A~%Nick: ~A~%Host: ~A~%Command: ~A~%Params: ~A~%"
				  user nickname host command params)))))


Annotations for this paste:

Annotation number 1: example
Pasted by: Cin
When:2 years, 2 months ago
Share:Tweet this! | http://paste.lisp.org/+UXK#1
Paste contents:
Raw Source | Display As
CL-USER> (parse-message ":Cin!cin@unaffilited/cin PRIVMSG #bloc")
User: cin
Nick: Cin
Host: unaffilited/cin
Command: PRIVMSG
Params:  #bloc
NIL

Colorize as:
Show Line Numbers
Index of paste annotations: 1

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.