Don't search in `buffer-string`.  You're creating a full copy of the whole contents of the buffer with all properties and stuff.

Instead, `(goto-char (point-min))` and then call `re-search-forward` with NOERROR non-nil.  Wrap the whole thing into `save-excursion`.

In either case, you **must** wrap the function in `save-match-data` to preserve the match data for other functions.