Lately, I have seen some cases where I use re-search-forward to find a pattern, and then try to do something with the match, only to find that the match-data is apparently not correct.
For example, in a file I might have this text:
:RESULTS:
async-abcd-1234-output
:END:
I would expect this code to replace async-abcd-1234-output with "string"
(when (re-search-forward "async-abcd-1234-\\(output\\|value\\)" nil t)
(let ((result-type (match-string 1)))
(cond ((string= result-type "output")
(replace-match "string")))))
That works when I run it in a buffer, but when I run it from a callback function I find that (match-string 1) is nil, and even (match-string 0) is "". What could be causing this? I feel like sometimes it works, and then stops working. It is baffling. Any ideas on how to debug this? When I use edebug, I can see the search appears to work, but the match-data isn't correct.