CINXE.COM
Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
<!DOCTYPE html> <html lang="en"><head><meta charset="utf-8"><link rel=canonical href='https://www.regular-expressions.info/'><title>Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="author" content="Jan Goyvaerts"> <meta name="description" content="At Regular-Expressions.info you will find a wide range of in-depth information about a powerful search pattern language called regular expressions."> <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>Welcome</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=topad style="height:130px"><A HREF="https://www.regexbuddy.com/create.html" TARGET="_top"><picture><source media="(max-width: 370px)" srcset="ads/320/rxbtutorial100.png 1x, ads/320/rxbtutorial150.png 1.5x, ads/320/rxbtutorial200.png 2x, ads/320/rxbtutorial250.png 2.5x, ads/320/rxbtutorial300.png 3x, ads/320/rxbtutorial350.png 3.5x, ads/320/rxbtutorial400.png 4x"><source media="(max-width: 500px)" srcset="ads/360/rxbtutorial100.png 1x, ads/360/rxbtutorial150.png 1.5x, ads/360/rxbtutorial200.png 2x, ads/360/rxbtutorial250.png 2.5x, ads/360/rxbtutorial300.png 3x, ads/360/rxbtutorial350.png 3.5x, ads/360/rxbtutorial400.png 4x"><source media="(max-width: 660px)" srcset="ads/480/rxbtutorial100.png 1x, ads/480/rxbtutorial150.png 1.5x, ads/480/rxbtutorial200.png 2x, ads/480/rxbtutorial250.png 2.5x, ads/480/rxbtutorial300.png 3x, ads/480/rxbtutorial350.png 3.5x, ads/480/rxbtutorial400.png 4x"><source media="(max-width: 747px)" srcset="ads/640/rxbtutorial100.png 1x, ads/640/rxbtutorial150.png 1.5x, ads/640/rxbtutorial200.png 2x, ads/640/rxbtutorial250.png 2.5x, ads/640/rxbtutorial300.png 3x, ads/640/rxbtutorial350.png 3.5x, ads/640/rxbtutorial400.png 4x"><img src="ads/728/rxbtutorial100.png" srcset="ads/728/rxbtutorial100.png 1x, ads/728/rxbtutorial125.png 1.25x, ads/728/rxbtutorial150.png 1.5x, ads/728/rxbtutorial175.png 1.75x, ads/728/rxbtutorial200.png 2x, ads/728/rxbtutorial250.png 2.5x, ads/728/rxbtutorial300.png 3x, ads/728/rxbtutorial350.png 3.5x, ads/728/rxbtutorial400.png 4x" alt="RegexBuddy—Better than a regular expression tutorial!"></picture></A></div> <div class=bulb><h1>Welcome to Regular-Expressions.info<br>The Premier website about Regular Expressions</h1><script type="text/javascript">showbulb();</script></div> <p>A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager. The regex equivalent is <TT CLASS=syntax><SPAN CLASS="regexspecial">^</SPAN><SPAN CLASS="regexspecial">.</SPAN><SPAN CLASS="regexspecial">*</SPAN><SPAN CLASS="regexescaped">\.</SPAN><SPAN CLASS="regexplain">txt</SPAN><SPAN CLASS="regexspecial">$</SPAN></TT>.</p> <p>But you can do much more with regular expressions. In a text editor like <A HREF="editpadpro.html" TARGET="_top">EditPad Pro</A> or a specialized text processing tool like <A HREF="powergrep.html" TARGET="_top">PowerGREP</A>, you could use the regular expression <TT CLASS=syntax><SPAN CLASS="regexspecial">\b</SPAN><SPAN CLASS="regexccopen">[</SPAN><SPAN CLASS="regexccrange">A-Z</SPAN><SPAN CLASS="regexccrange">0-9</SPAN><SPAN CLASS="regexccliteral">._%+</SPAN><SPAN CLASS="regexccliteral">-</SPAN><SPAN CLASS="regexccopen">]</SPAN><SPAN CLASS="regexspecial">+</SPAN><SPAN CLASS="regexplain">@</SPAN><SPAN CLASS="regexccopen">[</SPAN><SPAN CLASS="regexccrange">A-Z</SPAN><SPAN CLASS="regexccrange">0-9</SPAN><SPAN CLASS="regexccliteral">.</SPAN><SPAN CLASS="regexccliteral">-</SPAN><SPAN CLASS="regexccopen">]</SPAN><SPAN CLASS="regexspecial">+</SPAN><SPAN CLASS="regexescaped">\.</SPAN><SPAN CLASS="regexccopen">[</SPAN><SPAN CLASS="regexccrange">A-Z</SPAN><SPAN CLASS="regexccopen">]</SPAN><SPAN CLASS="regexspecial">{2,}</SPAN><SPAN CLASS="regexspecial">\b</SPAN></TT> to search for an email address. <i>Any</i> email address, to be exact. A very similar regular expression (replace the first <TT CLASS=syntax><SPAN CLASS="regexspecial">\b</SPAN></TT> with <TT CLASS=syntax><SPAN CLASS="regexspecial">^</SPAN></TT> and the last one with <TT CLASS=syntax><SPAN CLASS="regexspecial">$</SPAN></TT>) can be used by a programmer to check whether the user entered a <A HREF="email.html" TARGET="_top">properly formatted email address</A>. In just one line of code, whether that code is written in <A HREF="perl.html" TARGET="_top">Perl</A>, <A HREF="php.html" TARGET="_top">PHP</A>, <A HREF="java.html" TARGET="_top">Java</A>, <A HREF="dotnet.html" TARGET="_top">a .NET language</A>, or a multitude of other languages.</p> <h3>Regular Expressions Quick Start</h3> <p>If you just want to get your feet wet with regular expressions, take a look at the <A HREF="quickstart.html" TARGET="_top">one-page regular expressions quick start</A>. While you can’t learn to efficiently use regular expressions from this brief overview, it’s enough to be able to throw together a bunch of simple regular expressions. Each section in the quick start links directly to detailed information in the tutorial.</p> <h2>Complete Regular Expressions Tutorial</h2> <p>Do not worry if the above example or the quick start make little sense to you. Any non-trivial regex looks daunting to anybody not familiar with them. But with just a bit of experience, you will soon be able to craft your own regular expressions like you have never done anything else. The <A HREF="tutorial.html" TARGET="_top">free Regular-Expressions.info Tutorial</A> explains everything bit by bit.</p> <p>This tutorial is quite unique because it not only explains the regex syntax, but also describes in detail how the regex engine actually goes about its work. You will learn quite a lot, even if you have already been using regular expressions for some time. This will help you to understand quickly why a particular regex does not do what you initially expected, saving you lots of guesswork and head scratching when writing more complex regexes.</p> <h3>Replacement Strings Tutorial</h3> <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 website also includes a complete <A HREF="replacetutorial.html" TARGET="_top">replacement strings tutorial</A> that 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> <h2>Applications & Languages That Support Regexes</h2> <p>There are many <A HREF="tools.html" TARGET="_top">software applications and programming languages</A> that support regular expressions. If you are a programmer, you can save yourself lots of time and effort. You can often accomplish with a single regular expression in one or a few lines of code what would otherwise take dozens or hundreds.</p> <p>Many applications and programming languages have their own implementation of regular expressions, often with slight and sometimes with significant differences from other implementations. When two applications use a different implementation of regular expressions, we say that they use different “regular expression flavors”. Unlike most other regex tutorials, the tutorial on this website covers all the popular regular expression flavors, and indicates the differences that you should watch out for.</p> <h2>Not Only for Programmers</h2> <p>If you are not a programmer, you can use regular expressions in many situations just as well. They make finding information a lot easier. You can use them in powerful <A HREF="replacetutorial.html" TARGET="_top">search and replace</A> operations to quickly make changes across large numbers of files. A simple example is <TT CLASS=syntax><SPAN CLASS="regexplain">gr</SPAN><SPAN CLASS="regexccopen">[</SPAN><SPAN CLASS="regexccliteral">ae</SPAN><SPAN CLASS="regexccopen">]</SPAN><SPAN CLASS="regexplain">y</SPAN></TT> which finds both spellings of the word gray in one operation, instead of two. There are many <A HREF="editpadpro.html" TARGET="_top">text editors</A> and <A HREF="powergrep.html" TARGET="_top">search and replace tools</A> with decent regex support.</p> <h3>Further Reading</h3> <p>If you’re hungry for more information on regular expressions after reading this website, there are a <A HREF="books.html" TARGET="_top">variety of books</A> on the subject.</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='quickstart.html'>Regular Expressions Quick Start</a> | <a href='tutorial.html'>Regular Expressions Tutorial</a> | <a href='replacetutorial.html'>Replacement Strings Tutorial</a> | <a href='tools.html'>Applications and Languages</a> | <a href='examples.html'>Regular Expressions Examples</a> | <a href='refflavors.html'>Regular Expressions Reference</a> | <a href='refreplace.html'>Replacement Strings Reference</a> | <a href='books.html'>Book Reviews</a> | <a href='print.html'>Printable PDF</a> | <a href='about.html'>About This Site</a> | <a href='updates.html'>RSS Feed & Blog</a> |</p></div> <div id=copyright> <P CLASS=copyright>Page URL: <A HREF="https://www.regular-expressions.info/" TARGET="_top">https://www.regular-expressions.info/</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>