1

I try to understand the functions match-string and string-match in Emacs Lisp.

The manual provides what seems to be (?) a reproducible example:

     (string-match "\\(qu\\)\\(ick\\)"
                   "The quick fox jumped quickly.")
                   ;0123456789
          ⇒ 4
     
     (match-string 0 "The quick fox jumped quickly.")
          ⇒ "quick"

But this is not what I get if I try to execute those instructions. If I simply copy-paste them into a buffer and execute them with C-x C-e, the first one is OK and actually returns 4, but the second one triggers an error:

Debugger entered--Lisp error: (args-out-of-range "The quick fox jumped quickly." 341 341)
  match-string(0 "The quick fox jumped quickly.")
  eval((match-string 0 "The quick fox jumped quickly.") nil)
  elisp--eval-last-sexp(nil)
  eval-last-sexp(nil)
  funcall-interactively(eval-last-sexp nil)
  call-interactively(eval-last-sexp nil nil)
  command-execute(eval-last-sexp)

All the following examples presented in the manual also trigger similar errors.

I'm using Emacs 26.3. Is there a subtlety I don't understand (e.g., should match-string be called in a specific context)?

Drew
  • 75,699
  • 9
  • 109
  • 225
Philopolis
  • 1,094
  • 8
  • 14

0 Answers0