I'm trying to test for whether a string contains a positive integer (1, 2, 3, ...). By "string contains" I mean the string is only numbers (e.g. "1", "12", "123" but not "0", "1.1", "1a", "a1").
My first thought was to test the type and check if it is an integer but first I need to convert to numeric.
(typep (string-to-number "23K") 'integer)
;; t
This doesn't work because string-to-number
ignores any trailing characters.
Then I thought I'd use regular expressions to test that every character is in [0-9]
.
(equal (nth 0 (re-seq "[0-9]+" "23K")) "23K")
where re-seq
is taken from this post and searches for every occurrence of the regular expression (first arg) in the second arg.
Combining that with a test for the number being greater than 0 I end up with this:
(and (equal (nth 0 (re-seq "[0-9]+" "23K")) "23K")
(> (string-to-number "23K") 0) )
I'm pretty new to lisp programming so I'm wondering if there is a simpler way to do this.