3

I have this code

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

(string-match-test)

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.

Gabriele Nicolardi
  • 1,199
  • 8
  • 17

1 Answers1

3

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.
Marco Wahl
  • 2,796
  • 11
  • 13