Pascal
Pascal

Reputation: 443

String comparison mistake

I tried to write a set of functions that check the expiration date for a domain name:

(ql:quickload 'inferior-shell)

(defun whois-lookup (site)
  (let ((request (format nil "whois ~a" site)))
    (inferior-shell:run/ss request)))

(defun match-expiration-string (input)
  (let ((test-string "Registrar Registration Expiration Date:"))
    (string> input test-string)))

(defun domain-expiration-date (site)
  (with-input-from-string (input (whois-lookup site))
    (loop for line = (read-line input nil nil)
          while line do
            (when (match-expiration-string line)
              (format t "~A~%~t~A ~%" site line)))))

I'd call it like this: (domain-expiration-date "startpage.com").

Unfortunately, instead of just displaying the relevant line, it shows all of them.

match-expiration-string seems to be working correctly, so I have no idea what the problem is.

CL-USER> (match-expiration-string "Registrar Registration Expiration Date: 2016-05")
39 (6 bits, #x27, #o47, #b100111)
CL-USER> (match-expiration-string "Registrar Registration Expiration ")
NIL

Upvotes: 0

Views: 70

Answers (1)

Pascal
Pascal

Reputation: 443

As suggested by jkiiski, it works with a regular expression:

(defun match-expiration-string (input)
  (let ((test-string "Registrar Registration Expiration Date:"))
    (ppcre:scan test-string input)))

==>

CL-USER> (domain-expiration-date "startpage.com")
startpage.com
 Registrar Registration Expiration Date: 2018-10-10T04:00:00Z 
NIL

Like Joshua Taylor says, you don't need regex, only search. I also noticed that "Registrar Registration Expiration Date:" wasn't in every whois response, so I modified the search (and made place in case I needed other search strings for other types of domains):

(defun match-expiration-string (input)
  (let ((inclusion-strings '("Expiration Date:"))
        (exclusion-strings '("Registrar Registration Expiration Date:")))
    (when (some #'(lambda (s) (search s input))
                inclusion-strings)
      (notany #'(lambda (s) (search s input))
              exclusion-strings))))

Upvotes: 2

Related Questions