CINXE.COM
Replacement Text Tutorial
<!DOCTYPE html> <html lang="en"><head><meta charset="utf-8"><link rel=canonical href='https://https://www.regular-expressions.info//replacetutorial.html'><title>Replacement Text Tutorial</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="author" content="Jan Goyvaerts"> <meta name="description" content=""> <meta name="keywords" content=""> <link rel=stylesheet href="regex.css" type="text/css"><script src="theme.js" type="text/javascript"></script><link rel="alternate" type="application/rss+xml" title="New at Regular-Expressions.info" href="updates.xml"> </head> <body bgcolor=white text=black> <div id=top></div> <div id=btntop><div id=btngrid><a href="quickstart.html" target="_top"><div>Quick Start</div></a><a href="tutorial.html" target="_top"><div>Tutorial</div></a><a href="tools.html" target="_top"><div>Tools & Languages</div></a><a href="examples.html" target="_top"><div>Examples</div></a><a href="refflavors.html" target="_top"><div>Reference</div></a><a href="books.html" target="_top"><div>Book Reviews</div></a></div></div> <div id=contents><div id=side> <TABLE CLASS=side CELLSPACING=0 CELLPADDING=4><TR><TD CLASS=sideheader>Replacement Text Tutorial</TD></TR><TR><TD><A HREF="replacetutorial.html" TARGET=_top>Introduction</A></TD></TR><TR><TD><A HREF="replacecharacters.html" TARGET=_top>Characters</A></TD></TR><TR><TD><A HREF="replacenonprint.html" TARGET=_top>Non-Printable Characters</A></TD></TR><TR><TD><A HREF="replacematch.html" TARGET=_top>Matched Text</A></TD></TR><TR><TD><A HREF="replacebackref.html" TARGET=_top>Backreferences</A></TD></TR><TR><TD><A HREF="replacecontext.html" TARGET=_top>Match Context</A></TD></TR><TR><TD><A HREF="replacecase.html" TARGET=_top>Case Conversion</A></TD></TR><TR><TD><A HREF="replaceconditional.html" TARGET=_top>Conditionals</A></TD></TR> </TABLE><TABLE CLASS=side CELLSPACING=0 CELLPADDING=4><TR><TD CLASS=sideheader>More on This Site</TD></TR><TR><TD><A HREF="index.html" TARGET=_top>Introduction</A></TD></TR><TR><TD><A HREF="quickstart.html" TARGET=_top>Regular Expressions Quick Start</A></TD></TR><TR><TD><A HREF="tutorial.html" TARGET=_top>Regular Expressions Tutorial</A></TD></TR><TR><TD><A HREF="replacetutorial.html" TARGET=_top>Replacement Strings Tutorial</A></TD></TR><TR><TD><A HREF="tools.html" TARGET=_top>Applications and Languages</A></TD></TR><TR><TD><A HREF="examples.html" TARGET=_top>Regular Expressions Examples</A></TD></TR><TR><TD><A HREF="refflavors.html" TARGET=_top>Regular Expressions Reference</A></TD></TR><TR><TD><A HREF="refreplace.html" TARGET=_top>Replacement Strings Reference</A></TD></TR><TR><TD><A HREF="books.html" TARGET=_top>Book Reviews</A></TD></TR><TR><TD><A HREF="print.html" TARGET=_top>Printable PDF</A></TD></TR><TR><TD><A HREF="about.html" TARGET=_top>About This Site</A></TD></TR><TR><TD><A HREF="updates.html" TARGET=_top>RSS Feed & Blog</A></TD></TR></TABLE></DIV><div class=bodytext><div class=bulb><h1>Replacement Strings Tutorial</h1><script type="text/javascript">showbulb();</script></div> <p>A replacement string, also known as the replacement text, is the text that each regular expression match is replaced with during a search-and-replace. In most applications, the replacement text supports special syntax that allows you to reuse the text matched by the regular expression or parts thereof in the replacement. This tutorial explains this syntax. While replacement strings are fairly simple compared with regular expressions, there is still great variety between the syntax used by various applications and their actual behavior.</p> <p>On this website, replacement strings are shown as <TT CLASS=syntax><SPAN CLASS="replaceplain">replace</SPAN></TT> like you would enter them in the Replace box of an application. Literal text in the replacement is highlighted in yellow. As <TT CLASS=syntax><SPAN CLASS="regexspecial">$&</SPAN><SPAN CLASS="regexescaped">\$</SPAN></TT> shows, special tokens are highlighted in blue and escaped characters in gray.</p> <h2>Table of Contents</h2> <p><A HREF="replacecharacters.html" TARGET="_top">Literal Characters and Special Characters</A></p> <p class=indent>The simplest replacement text consists of only literal characters. Certain characters have special meanings in replacement strings and have to be escaped. Escaping rules may get a bit complicated when using replacement strings in software source code.</p> <p><A HREF="nonprint.html" TARGET="_top">Non-Printable Characters</A></p> <p class=indent>Non-printable characters such as control characters and special spacing or line break characters are easier to enter using control character escapes or hexadecimal escapes.</p> <p><A HREF="replacematch.html" TARGET="_top">Matched Text</A></p> <p class=indent>Reinserting the entire regex match into the replacement text allows a search-and-replace to insert text before and after regular expression matches without really replacing anything.</p> <p><A HREF="replacebackref.html" TARGET="_top">Backreferences</A></p> <p class=indent>Backreferences to <A HREF="named.html" TARGET="_top">named</A> and <A HREF="brackets.html" TARGET="_top">numbered capturing groups</A> in the regular expression allow the replacement text to reuse parts of the text matched by the regular expression.</p> <p><A HREF="replacecontext.html" TARGET="_top">Match Context</A></p> <p class=indent>Some applications support special tokens in replacement strings that allow you to insert the subject string or the part of the subject string before or after the regex match. This can be useful when the replacement text syntax is used to collect search matches and their context instead of making replacements in the subject string.</p> <p><A HREF="replacecase.html" TARGET="_top">Case Conversion</A></p> <p class=indent>Some applications can insert the text matched by the regex or by capturing groups converted to uppercase or lowercase.</p> <p><A HREF="replaceconditional.html" TARGET="_top">Conditionals</A></p> <p class=indent>Some applications can use one replacement or another replacement depending on whether a capturing group participated in the match. This allows you to use different replacements for different matches of the regular expression.</p><div id=cntmobi><p>| <a href='quickstart.html'>Quick Start</a> | <a href='tutorial.html'>Tutorial</a> | <a href='tools.html'>Tools & Languages</a> | <a href='examples.html'>Examples</a> | <a href='refflavors.html'>Reference</a> | <a href='books.html'>Book Reviews</a> |</p><p>| <a href='replacetutorial.html'>Introduction</a> | <a href='replacecharacters.html'>Characters</a> | <a href='replacenonprint.html'>Non-Printable Characters</a> | <a href='replacematch.html'>Matched Text</a> | <a href='replacebackref.html'>Backreferences</a> | <a href='replacecontext.html'>Match Context</a> | <a href='replacecase.html'>Case Conversion</a> | <a href='replaceconditional.html'>Conditionals</a> |</p></div> <div id=copyright> <P CLASS=copyright>Page URL: <A HREF="https://www.regular-expressions.info/replacetutorial.html" TARGET="_top">https://www.regular-expressions.info/replacetutorial.html</A><BR> Page last updated: 12 August 2021<BR> Site last updated: 06 November 2024<BR> Copyright © 2003-2024 Jan Goyvaerts. All rights reserved.</P> </div> </div> </div> </body></html>