Re: looking-back

Michael Heerdegen via Users list for the GNU Emacs text editor <[email protected]> Sat, 01 Aug 2026 20:12:47 +0200
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
Andreas Röhler <[email protected]> writes:

> want to match an opening paren followed by a whitespace. Tried the
> forms below without success.
> Any suggestion?

Honestly: give up trial and error.

> ( (looking-back "\\\\( +" (line-beginning-position) t)  ==> nil
> ( (looking-back (regexp-quote (rx "(" (+ space))) (line-beginning-position) t) ==> nil

Using `rx' is a good idea.

Only some hints:

`rx' returns ready-to-use regexps (of course!), no need for another
`regexp-quote' wrapper.  You are using a regexp that matches occurrences of
the regexp you want, not of the text you actually want to match.

`looking-back' is expensive.  It's better to use

| (save-excursion (goto-char ...) (looking-at ...))

wherever possible.  "possible" most of the time means you know where the
matched text starts.


Michael.