EmilySeville7cfg · 2022-07-30 02:47
Why `match-string` requires STRING argument but `match-beginning` doesn't?
7890yuiop · 2022-07-30 02:49
Assuming you mean "when searching in a string" the answer is that `match-beginning` is only returning an index, which the match data provides directly.
Phil-Hudson · 2022-07-30 15:00
To expand on this, the "match data" does *not* contain the text against which the the most recent `string-match` was carried out. As I understand it (corrections from better-informed Lispers welcome), the "match data" is a complex data structure which was designed to match against the current buffer, and only gained the ability to match against an arbitrary string later. Thus the match data contains offsets (or "markers") into the text indicating where (sub-)matches begin and end. Functions like `match-string` therefore assume those markers apply to the current buffer, *unless* you re-supply the original string for those offsets to be applied to when you call `match-string`.
7890yuiop · 2022-07-31 03:32
And in case it's still unclear:
(let ((str "hello"))
(when (string-match "^h.." str)
(match-string 0 str)))
"hel"
(let ((str "hello"))
(when (string-match "^h.." str)
(match-string 0 "goodbye")))
"goo"