Determine if a string is numeric: Difference between revisions
Content deleted Content added
→{{header|jq}}: gojq and jaq too |
added code for testing strings |
||
Line 2,037: | Line 2,037: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Below is ELisp code to test two lists. One is a list of strings that |
|||
would be acceptable numbers in Emacs ELisp. The second is a list of strings |
|||
that are not valid numbers. The code tests each string in each list. |
|||
<syntaxhighlight lang="lisp"> |
|||
(setq valid-strings '("3" "0" "-0" "2." "1000" "-4" "-5." "6.2" "-8.45" "+15e2" "-15e2" "#b101100" "#o54" "#x2c" "1500.0" "#24r1k" "3")) |
|||
(setq invalid-strings '("3cat" "1,000" "5.6.7" "cat3" "def" "zero")) |
|||
(with-current-buffer (pop-to-buffer "my-test") |
|||
(erase-buffer) |
|||
(insert "Test for valid strings:\n") |
|||
(dolist (test-string valid-strings) |
|||
(let ((test-result)) |
|||
(setq test-result (string-valid-number-p test-string)) |
|||
(insert (format "%-8s - %s \n" test-string test-result)))) |
|||
(insert "\n" "\n") |
|||
(insert "Test for invalid strings:\n") |
|||
(dolist (test-string invalid-strings) |
|||
(let ((test-result)) |
|||
(setq test-result (string-valid-number-p test-string)) |
|||
(insert (format "%-5s - %s \n" test-string test-result))))) |
|||
</syntaxhighlight> |
|||
Below is the result of the tests: |
|||
<pre> |
<pre> |
||
Test for valid strings: |
Test for valid strings: |