How can I get my target string point/marker without moving the pointer?

Lets say I have the following LaTeX code:

\ref{AAA}, \ref{BBB} ... \ref{BBB,CCC,DDD}

\begin{the bibliography}{99}

\bibitem{AAA}

\bibitem{BBB}


and I want to write a function that does:

(perform-replace "\\<AAA\\>" "\\<BBB\\>" t t nil 1 nil (point-min) BIB_BEG)


(asks if I want to change "AAA" --> "BBB" before BIB_BEG marker)

To get BIB_BEG I usually do (something like):

(let ((BIB_BEG make-marker))
(goto-char (point-min))
(search-forward "\\begin{thebibliography}" nil t)
(set-marker BIB_BEG (match-beginning 0))
;; perform-replace stuff
)


Now I was trying to do something like (I know it's wrong):

(let ((BIB_BEG make-marker))
(string-match "\\begin{thebibliography}" (buffer-string))
(set-marker BIB_BEG (match-beginning 0))
;; perform-replace stuff
)


My question is, how can I get my target point/marker without moving the pointer? I already know I can use (save-excusion) to restore the initial position. In other words I was wondering if ther is a way to get the position I need with something like the string-match tecnique.

• Could you provide some example before-after? M-x query-replace-regexp RET or M-x query-replace RET shall be options to use, – Andreas Röhler Feb 11 '18 at 19:39
• @AndreasRöhler Not in my case... I need to do it with a script. – Gabriele Nicolardi Feb 11 '18 at 20:42
• Sounds like you're just looking for save-excursion. Wrap your point movement with that and when it's done point is restored where it was. – Drew Feb 11 '18 at 22:58