My text contains several occurrences like "(1)", "(2)", "(3)". I want to replace these with the general format like "fixed text (1)", "fixed text (2)", "fixed text (3)" using Search and Replace.

Can I do this in one go using perhaps regular expressions?

  • 3
    Read the "Searching and Replacement" section of the Emacs manual. You can do that from inside Emacs with C-h i g(emacs)Searching and Replacement. In particular, read about query-replace-regexp. You can use ([0-9]) as the regexp to search for: it matches a parenthesized single digit. You can use fixed text\& for the replacement part: the \& is itself replaced by whatever matched, so it is (1) for the first replacement, (2) for the second and so on. Be sure to put your cursor at the top of the buffer in order to catch everything.
    – NickD
    Sep 9, 2021 at 16:57
  • Does this answer your question? Replace full-width numerals with half-width numerals
    – Drew
    Sep 9, 2021 at 17:09
  • @Drew Yes and no. Yes in the sense that the knowledge presented in the corresponding answer can be used to solve the problem in the current question. No, in the sense that the mentioned answer is too complex for the general reader to apply to the current problem. Hence, I would suggest that we keep the present question as well.
    – Masroor
    Sep 10, 2021 at 0:20
  • Be sure to use ([0-9]+) rather than ([0-9]) if your numbers can be more than one digit.
    – phils
    Sep 10, 2021 at 4:14

1 Answer 1


M-C-% \(([[:digit:]]+)\) RET fixed text \1 RET !

RET means to press the return key.

Some Explanation

  • M-C-% runs the command query-replace-regexp in a typical Emacs.
  • \(([[:digit:]]+)\) is the regexp for the occurances to be found like e.g. (1), (2) or (42).
    • [:digit:] is the character class for digits. See e.g. the Elisp info pages for more.
    • [[:digit:]] stands for any single digit in the regexp like [0-9].
    • [[:digit:]]+ stands for any sequence of digits.
    • ([[:digit:]]+) stands for any sequence of digits within parenthesis.
    • And the surrounding quoted parenthesis define group 1 for each match which allows to reference the match in the replacement part. See below.
  • fixed text \1 is the replacement part which consists of the fixed text and
    • \1 which is the way to reference the group 1 match.
  • The final ! let's Emacs replace all further occurances in one go.


Note the hint in the comments about \&. \& in the replacement part stands for the whole match. This is applicable in the given case. Explicit grouping is not necessary here.

Concretely one could use

M-C-% ([[:digit:]]+) RET fixed text \& RET !

for the given task.

The explicit grouping is more flexible, tho.

  • 1
    Nice! Can you explain why the doubled []?
    – Rusi
    Sep 12, 2021 at 14:02
  • 1
    Please a bit of explanation before I can accept the answer.
    – Masroor
    Sep 12, 2021 at 14:34
  • Thanks. I elaborated some...
    – Marco Wahl
    Sep 12, 2021 at 18:35
  • 1
    Note that M-x re-builder is a great feature to experiment a regexp over a document, interactively.
    – gigiair
    Sep 13, 2021 at 7:21

Your Answer

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

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