I want to find the first occurrence of an uppercase letter. I am trying to use re-search-backwards; yet it is not working as intended.

Here's an example:

(defun test ()
    (re-search-backward "[:upper:]"))

When used in the following phrase (standing at the end; aka the final "r"):

hi, my Name is peter

The cursor goes to "e". How can I make the cursor go to "N".

Using re-search-backwards preferably

  • 1
    The syntax is [[:upper:]].
    – NickD
    Apr 5 at 16:42

2 Answers 2


In addition to fixing the syntax of the regexp, you also need to disable case-folding:

  (defun my/test ()
      (let ((case-fold-search nil))
        (re-search-backward "[[:upper:]]")))

Check the docstring of the function C-h f re-search-backward. It tells you that the function behaves like re-search-forward except in search direction, so you also need to read the docstring of that with C-h f re-search-forward which says:

Search case-sensitivity is determined by the value of the variable ‘case-fold-search’, which see.

So do C-h v case-fold-search to find out how to deal with case-sensitive searches. The idiom above is standard and very often useful.

For the regexp syntax, read the Regexp Syntax section of the manual, or better, do C-h i g(emacs) RET i regexp in your Emacs. Learning to use Info in Emacs is very useful.

  • Thank you. I was missing the case fold search variable Apr 9 at 14:12

As @NickD said in a comment, you need to use "[[:upper:]]", not "[:upper:]".

  • The result remains the same. It does not go backwards to the N Apr 5 at 17:16
  • I cannot reproduce this: with the correct syntax (and ensuring the case-fold-search is nil), running this function takes me to the N. Apr 5 at 19:05
  • What @FranBurstall said. If you don't see this then make sure you're starting with vanilla Emacs: emacs -Q (no init file). If the problem is in your init file then bisect it to find the culprit.
    – Drew
    Apr 5 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.