RosettaCodeData/Task/Determine-if-a-string-is-numeric/Clojure/determine-if-a-string-is-numeric-3.clj
2023-07-01 13:44:08 -04:00

11 lines
412 B
Clojure

(require '[clojure.edn :as edn])
(import [java.io PushbackReader StringReader])
(defn number-string? [s]
(boolean
(when (and (string? s) (re-matches #"^[+-]?\d.*" s))
(let [reader (PushbackReader. (StringReader. s))
num (try (edn/read reader) (catch Exception _ nil))]
(when num
; Check that the string has nothing after the number
(= -1 (.read reader)))))))