<p>I want to find any text in a file that matches a regexp of the form <em>t</em><code>[A-Z]</code><em>u</em> (i.e., a match <em>t</em> followed by a capital letter and another match <em>u</em>, and transform the matched text so that the capital letter is lowercase. For example, for the regexp <code>x[A-Z]y</code></p>

<pre><code>xAy
</code></pre>

<p>becomes</p>

<pre><code>xay
</code></pre>

<p>and</p>

<pre><code>xZy
</code></pre>

<p>becomes</p>

<pre><code>xzy
</code></pre>

<p>Emacs' <code>query-replace</code> function allows back-references, but AFAIK not the transformation of the matched text. Is there a built-in function that does this? Does anybody have a short Elisp function I could use?</p>

<p><strong>UPDATE</strong></p>

<p>@Marcel Levy has it: <code>\,</code> in a replacement expression introduces an (arbitrary?) Elisp expression. E.g., the solution to the above is</p>

<pre><code>M-x replace-regexp &lt;RET&gt; x\([A-Z]\)z &lt;RET&gt; x\,(downcase \1)z
</code></pre>

[{'answer_id': 57794, 'author': 'Marcel Levy', 'author_id': 676, 'author_profile': 'https://Stackoverflow.com/users/676', 'pm_score': 5, 'selected': True, 'text': '<p>It looks like <a href="http://steve-yegge.blogspot.com/" rel="noreferrer">Steve Yegge</a> actually already posted the answer to this a few years back: <a href="http://steve-yegge.blogspot.com/2006/06/shiny-and-new-emacs-22.html" rel="noreferrer">"Shiny and New: Emacs 22."</a> Scroll down to "Changing Case in Replacement Strings" and you\'ll see his example code using the <code>replace-regexp</code> function.</p>\n\n<p>The general answer is that you use "\\," to call any lisp expression as part of the replacement string, as in <code>\\,(capitalize \\1)</code>. Reading the help text, it looks like it\'s only in interactive mode, but that seems like the one place where this would be most necessary.</p>\n'}, {'answer_id': 67756, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>An alternative to qrr in this case is recording a macro and replaying it. (isearch-forward-regexp, select the character, downcase-region.) I find on the fly macros easier, since you get immediate feedback if your regexp is wrong.</p>\n'}, {'answer_id': 69754, 'author': 'jfm3', 'author_id': 11138, 'author_profile': 'https://Stackoverflow.com/users/11138', 'pm_score': 0, 'selected': False, 'text': "<p>I'd do this with a macro as well, but only because executing code from within a replacement string for a regular expression is very unintuitive to me.  If you're writing a batch script or something that needs to go very fast, \\, is certainly the way to go.</p>\n"}]
['https://Stackoverflow.com/questions/57751', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1412/']