fork download
  1. (defun string-split-if (predicate string)
  2. (if (equal string "")
  3. (list (copy-seq string))
  4. (do* ((result '())
  5. (begin 0 (+ end 1))
  6. (end #1=(position-if predicate string :start begin) #1#))
  7. ((not end)
  8. (nreverse (push (subseq string begin) result)))
  9. (push (subseq string begin end) result))))
  10.  
  11. (defun extract-numbers (string &key (radix 10) (only-integers nil))
  12. (check-type radix (integer 2 36))
  13. (let ((*read-base* radix)
  14. (digits (concatenate 'string
  15. (subseq "0123456789abcdefghijklmnopqrstuvwxyz"
  16. 0 radix)
  17. (if only-integers "" "."))))
  18. (loop for x in (string-split-if (complement (lambda (c)
  19. (find c digits
  20. :test #'char-equal)))
  21. string)
  22. for n = (ignore-errors (read-from-string x nil nil))
  23. when (numberp n)
  24. collect n)))
  25.  
  26. (print (extract-numbers "「12あいうえお34.5かき7.0くけこ」"))
  27. (print (extract-numbers "「0.12ABC3.45DE0.02FGH」"))
  28. (print (extract-numbers "「0.12ABC3.45DE0.02FGH」" :radix 16))
  29. (print (extract-numbers "「0.12ABC3.45DE0.02FGH」" :radix 16 :only-integers t))
  30.  
Success #stdin #stdout 0.01s 25444KB
stdin
Standard input is empty
stdout
(12 34.5 7.0) 
(0.12 3.45 0.02) 
NIL 
(0 1223619 286176 47)