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.


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 '17 at 8:44
  • Yeah! So much to discover and learn. Same here. ;)
    – Marco Wahl
    Jul 15 '17 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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