1

I want to check if a user input a word or a number. And I write the function look like this:

(defun check-input (arg)
  "description"
  (interactive "senter arg: ")
  (setq isnum (string-match "\\'[0-9]+\\'" arg))
  (if isnum
      (message "INPUT is number ")
  (message "INPUT is string ")))

But every time I enter whether the a number of a word, it just show "Input is string". Can you help me where did I go wrong. Thanks you very much

1
  • 1
    1. What others have said, regarding your use of string-match. 2. Use string-match-p. 3. Bind isnum with let, instead of using a global variable - or just put the test sexp directly in your if (no variable isnum).
    – Drew
    Mar 28, 2017 at 14:00

3 Answers 3

1
(setq isnum (string-match "\\'[0-9]+\\'" arg))

That should be (setq isnum (string-match "\\`[0-9]+\\'" arg))

\\` means beginning of string, \\' means end of string.

See (elisp) Regexp Backslash.

0
0

Depending on what you are trying to accomplish here, you can use (string-to-number arg). This will return 0 if the argument is a string that cannot be parsed as a number; otherwise, it just returns the value converted to a number.

1
  • this worked too, thanks for your help
    – Quang Bui
    Mar 29, 2017 at 1:55
-1

You don't need the quotes or backslashes in your regexp. Change it to just (string-match "[0-9]+" arg) and I believe it ought to do what you want.

1
  • That would say "number" for inputs like abc123.
    – npostavs
    Mar 28, 2017 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.