I have this code

(defun string-match-test ()
  (string-match "12345" (buffer-string))
  (setq STRING (match-string 0))
  (read-string STRING)
  (goto-char (point-max))
  (insert STRING)


I expected it to print 12345 but I get, instead "1234. What's my mistake? May I have misunderstood the buffer-string function behaviour?

Note. I kwow I can use serch-forward-regexp combined with with-temp-buffer... but I'd like to understand the reason of this weird behaviur.

1 Answer 1


See the documentation of match-string. It contains:

(match-string NUM &optional STRING)
STRING should be given if the last search was by ‘string-match’ on STRING.
  • Thank you. Actually I thought that the current buffer and the buffer-string output was the same thing/string. Jul 14, 2017 at 8:44
  • Yeah! So much to discover and learn. Same here. ;)
    – Marco Wahl
    Jul 15, 2017 at 11:24

Your Answer

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

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