CINXE.COM
perlreref - Perl Regular Expressions Reference - Perldoc Browser
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>perlreref - Perl Regular Expressions Reference - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/perlreref"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.12.1</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/perlreref">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/perlreref">5.40.1</a> <a class="dropdown-item" href="/5.40.0/perlreref">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/perlreref">5.38.3</a> <a class="dropdown-item" href="/5.38.2/perlreref">5.38.2</a> <a class="dropdown-item" href="/5.38.1/perlreref">5.38.1</a> <a class="dropdown-item" href="/5.38.0/perlreref">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/perlreref">5.36.3</a> <a class="dropdown-item" href="/5.36.2/perlreref">5.36.2</a> <a class="dropdown-item" href="/5.36.1/perlreref">5.36.1</a> <a class="dropdown-item" href="/5.36.0/perlreref">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/perlreref">5.34.3</a> <a class="dropdown-item" href="/5.34.2/perlreref">5.34.2</a> <a class="dropdown-item" href="/5.34.1/perlreref">5.34.1</a> <a class="dropdown-item" href="/5.34.0/perlreref">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/perlreref">5.32.1</a> <a class="dropdown-item" href="/5.32.0/perlreref">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/perlreref">5.30.3</a> <a class="dropdown-item" href="/5.30.2/perlreref">5.30.2</a> <a class="dropdown-item" href="/5.30.1/perlreref">5.30.1</a> <a class="dropdown-item" href="/5.30.0/perlreref">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/perlreref">5.28.3</a> <a class="dropdown-item" href="/5.28.2/perlreref">5.28.2</a> <a class="dropdown-item" href="/5.28.1/perlreref">5.28.1</a> <a class="dropdown-item" href="/5.28.0/perlreref">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/perlreref">5.26.3</a> <a class="dropdown-item" href="/5.26.2/perlreref">5.26.2</a> <a class="dropdown-item" href="/5.26.1/perlreref">5.26.1</a> <a class="dropdown-item" href="/5.26.0/perlreref">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/perlreref">5.24.4</a> <a class="dropdown-item" href="/5.24.3/perlreref">5.24.3</a> <a class="dropdown-item" href="/5.24.2/perlreref">5.24.2</a> <a class="dropdown-item" href="/5.24.1/perlreref">5.24.1</a> <a class="dropdown-item" href="/5.24.0/perlreref">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/perlreref">5.22.4</a> <a class="dropdown-item" href="/5.22.3/perlreref">5.22.3</a> <a class="dropdown-item" href="/5.22.2/perlreref">5.22.2</a> <a class="dropdown-item" href="/5.22.1/perlreref">5.22.1</a> <a class="dropdown-item" href="/5.22.0/perlreref">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/perlreref">5.20.3</a> <a class="dropdown-item" href="/5.20.2/perlreref">5.20.2</a> <a class="dropdown-item" href="/5.20.1/perlreref">5.20.1</a> <a class="dropdown-item" href="/5.20.0/perlreref">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/perlreref">5.18.4</a> <a class="dropdown-item" href="/5.18.3/perlreref">5.18.3</a> <a class="dropdown-item" href="/5.18.2/perlreref">5.18.2</a> <a class="dropdown-item" href="/5.18.1/perlreref">5.18.1</a> <a class="dropdown-item" href="/5.18.0/perlreref">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/perlreref">5.16.3</a> <a class="dropdown-item" href="/5.16.2/perlreref">5.16.2</a> <a class="dropdown-item" href="/5.16.1/perlreref">5.16.1</a> <a class="dropdown-item" href="/5.16.0/perlreref">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/perlreref">5.14.4</a> <a class="dropdown-item" href="/5.14.3/perlreref">5.14.3</a> <a class="dropdown-item" href="/5.14.2/perlreref">5.14.2</a> <a class="dropdown-item" href="/5.14.1/perlreref">5.14.1</a> <a class="dropdown-item" href="/5.14.0/perlreref">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/perlreref">5.12.5</a> <a class="dropdown-item" href="/5.12.4/perlreref">5.12.4</a> <a class="dropdown-item" href="/5.12.3/perlreref">5.12.3</a> <a class="dropdown-item" href="/5.12.2/perlreref">5.12.2</a> <a class="dropdown-item active" href="/5.12.1/perlreref">5.12.1</a> <a class="dropdown-item" href="/5.12.0/perlreref">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/perlreref">5.10.1</a> <a class="dropdown-item" href="/5.10.0/perlreref">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/perlreref">5.8.9</a> <a class="dropdown-item" href="/5.8.8/perlreref">5.8.8</a> <a class="dropdown-item" href="/5.8.7/perlreref">5.8.7</a> <a class="dropdown-item" href="/5.8.6/perlreref">5.8.6</a> <a class="dropdown-item" href="/5.8.5/perlreref">5.8.5</a> <a class="dropdown-item" href="/5.8.4/perlreref">5.8.4</a> <a class="dropdown-item" href="/5.8.3/perlreref">5.8.3</a> <a class="dropdown-item" href="/5.8.2/perlreref">5.8.2</a> <a class="dropdown-item" href="/5.8.1/perlreref">5.8.1</a> <a class="dropdown-item" href="/5.8.0/perlreref">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/perlreref">5.6.2</a> <a class="dropdown-item" href="/5.6.1/perlreref">5.6.1</a> <a class="dropdown-item" href="/5.6.0/perlreref">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/perlreref">5.005_04</a> <a class="dropdown-item" href="/5.005_03/perlreref">5.005_03</a> <a class="dropdown-item" href="/5.005_02/perlreref">5.005_02</a> <a class="dropdown-item" href="/5.005_01/perlreref">5.005_01</a> <a class="dropdown-item" href="/5.005/perlreref">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/perlreref">blead</a> <a class="dropdown-item" href="/5.41.9/perlreref">5.41.9</a> <a class="dropdown-item" href="/5.41.8/perlreref">5.41.8</a> <a class="dropdown-item" href="/5.41.7/perlreref">5.41.7</a> <a class="dropdown-item" href="/5.41.6/perlreref">5.41.6</a> <a class="dropdown-item" href="/5.41.5/perlreref">5.41.5</a> <a class="dropdown-item" href="/5.41.4/perlreref">5.41.4</a> <a class="dropdown-item" href="/5.41.3/perlreref">5.41.3</a> <a class="dropdown-item" href="/5.41.2/perlreref">5.41.2</a> <a class="dropdown-item" href="/5.41.1/perlreref">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/perlreref">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/perlreref">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/perlreref">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/perlreref">5.39.10</a> <a class="dropdown-item" href="/5.39.9/perlreref">5.39.9</a> <a class="dropdown-item" href="/5.39.8/perlreref">5.39.8</a> <a class="dropdown-item" href="/5.39.7/perlreref">5.39.7</a> <a class="dropdown-item" href="/5.39.6/perlreref">5.39.6</a> <a class="dropdown-item" href="/5.39.5/perlreref">5.39.5</a> <a class="dropdown-item" href="/5.39.4/perlreref">5.39.4</a> <a class="dropdown-item" href="/5.39.3/perlreref">5.39.3</a> <a class="dropdown-item" href="/5.39.2/perlreref">5.39.2</a> <a class="dropdown-item" href="/5.39.1/perlreref">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/perlreref">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.12.1/perl">Perl</a> <a class="dropdown-item" href="/5.12.1/perlintro">Intro</a> <a class="dropdown-item" href="/5.12.1/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.12.1/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.12.1/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.1/perlop">Operators</a> <a class="dropdown-item" href="/5.12.1/functions">Functions</a> <a class="dropdown-item" href="/5.12.1/variables">Variables</a> <a class="dropdown-item" href="/5.12.1/modules">Modules</a> <a class="dropdown-item" href="/5.12.1/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.1/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.12.1/perlcommunity">Community</a> <a class="dropdown-item" href="/5.12.1/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.12.1/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.12.1/perlreref">perlreref</a> <div id="more"> (<a href="/5.12.1/perlreref.txt">source</a>, <a href="https://metacpan.org/pod/perlreref">CPAN</a>) </div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.12.1. <a href="/perlreref">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> <ul> <li> <a class="text-decoration-none" href="#OPERATORS">OPERATORS</a> </li> <li> <a class="text-decoration-none" href="#SYNTAX">SYNTAX</a> </li> <li> <a class="text-decoration-none" href="#ESCAPE-SEQUENCES">ESCAPE SEQUENCES</a> </li> <li> <a class="text-decoration-none" href="#CHARACTER-CLASSES">CHARACTER CLASSES</a> </li> <li> <a class="text-decoration-none" href="#ANCHORS">ANCHORS</a> </li> <li> <a class="text-decoration-none" href="#QUANTIFIERS">QUANTIFIERS</a> </li> <li> <a class="text-decoration-none" href="#EXTENDED-CONSTRUCTS">EXTENDED CONSTRUCTS</a> </li> <li> <a class="text-decoration-none" href="#VARIABLES">VARIABLES</a> </li> <li> <a class="text-decoration-none" href="#FUNCTIONS">FUNCTIONS</a> </li> <li> <a class="text-decoration-none" href="#TERMINOLOGY">TERMINOLOGY</a> <ul> <li> <a class="text-decoration-none" href="#Titlecase">Titlecase</a> </li> </ul> </li> </ul> </li> <li> <a class="text-decoration-none" href="#AUTHOR">AUTHOR</a> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> <li> <a class="text-decoration-none" href="#THANKS">THANKS</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>perlreref - Perl Regular Expressions Reference</p> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>This is a quick reference to Perl's regular expressions. For full information see <a href="/5.12.1/perlre">perlre</a> and <a href="/5.12.1/perlop">perlop</a>, as well as the <a href="#SEE-ALSO">"SEE ALSO"</a> section in this document.</p> <h2 id="OPERATORS"><a class="permalink" href="#OPERATORS">#</a>OPERATORS</h2> <p><code>=~</code> determines to which variable the regex is applied. In its absence, $_ is used.</p> <pre><code>$var =~ /foo/;</code></pre> <p><code>!~</code> determines to which variable the regex is applied, and negates the result of the match; it returns false if the match succeeds, and true if it fails.</p> <pre><code>$var !~ /foo/;</code></pre> <p><code>m/pattern/msixpogc</code> searches a string for a pattern match, applying the given options.</p> <pre><code class="plaintext">m Multiline mode - ^ and $ match internal lines s match as a Single line - . matches \n i case-Insensitive x eXtended legibility - free whitespace and comments p Preserve a copy of the matched string - ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be defined. o compile pattern Once g Global - all occurrences c don't reset pos on failed matches when using /g</code></pre> <p>If 'pattern' is an empty string, the last <i>successfully</i> matched regex is used. Delimiters other than '/' may be used for both this operator and the following ones. The leading <code>m</code> can be omitted if the delimiter is '/'.</p> <p><code>qr/pattern/msixpo</code> lets you store a regex in a variable, or pass one around. Modifiers as for <code>m//</code>, and are stored within the regex.</p> <p><code>s/pattern/replacement/msixpogce</code> substitutes matches of 'pattern' with 'replacement'. Modifiers as for <code>m//</code>, with one addition:</p> <pre><code class="plaintext">e Evaluate 'replacement' as an expression</code></pre> <p>'e' may be specified multiple times. 'replacement' is interpreted as a double quoted string unless a single-quote (<code>'</code>) is the delimiter.</p> <p><code>?pattern?</code> is like <code>m/pattern/</code> but matches only once. No alternate delimiters can be used. Must be reset with reset().</p> <h2 id="SYNTAX"><a class="permalink" href="#SYNTAX">#</a>SYNTAX</h2> <pre><code>\ Escapes the character immediately following it . Matches any single character except a newline (unless /s is used) ^ Matches at the beginning of the string (or line, if /m is used) $ Matches at the end of the string (or line, if /m is used) * Matches the preceding element 0 or more times + Matches the preceding element 1 or more times ? Matches the preceding element 0 or 1 times {...} Specifies a range of occurrences for the element preceding it [...] Matches any one of the characters contained within the brackets (...) Groups subexpressions for capturing to $1, $2... (?:...) Groups subexpressions without capturing (cluster) | Matches either the subexpression preceding or following it \1, \2, \3 ... Matches the text from the Nth group \g1 or \g{1}, \g2 ... Matches the text from the Nth group \g-1 or \g{-1}, \g-2 ... Matches the text from the Nth previous group \g{name} Named backreference \k<name> Named backreference \k'name' Named backreference (?P=name) Named backreference (python syntax)</code></pre> <h2 id="ESCAPE-SEQUENCES"><a class="permalink" href="#ESCAPE-SEQUENCES">#</a><a id="ESCAPE"></a>ESCAPE SEQUENCES</h2> <p>These work as in normal strings.</p> <pre><code class="plaintext">\a Alarm (beep) \e Escape \f Formfeed \n Newline \r Carriage return \t Tab \037 Any octal ASCII value \x7f Any hexadecimal ASCII value \x{263a} A wide hexadecimal value \cx Control-x \N{name} A named character \N{U+263D} A Unicode character by hex ordinal \l Lowercase next character \u Titlecase next character \L Lowercase until \E \U Uppercase until \E \Q Disable pattern metacharacters until \E \E End modification</code></pre> <p>For Titlecase, see <a href="#Titlecase">"Titlecase"</a>.</p> <p>This one works differently from normal strings:</p> <pre><code class="plaintext">\b An assertion, not backspace, except in a character class</code></pre> <h2 id="CHARACTER-CLASSES"><a class="permalink" href="#CHARACTER-CLASSES">#</a><a id="CHARACTER"></a>CHARACTER CLASSES</h2> <pre><code class="plaintext">[amy] Match 'a', 'm' or 'y' [f-j] Dash specifies "range" [f-j-] Dash escaped or at start or end means 'dash' [^f-j] Caret indicates "match any character _except_ these"</code></pre> <p>The following sequences (except <code>\N</code>) work within or without a character class. The first six are locale aware, all are Unicode aware. See <a href="/5.12.1/perllocale">perllocale</a> and <a href="/5.12.1/perlunicode">perlunicode</a> for details.</p> <pre><code class="plaintext"> \d A digit \D A nondigit \w A word character \W A non-word character \s A whitespace character \S A non-whitespace character \h An horizontal whitespace \H A non horizontal whitespace \N A non newline (when not followed by '{NAME}'; experimental; not valid in a character class; equivalent to [^\n]; it's like '.' without /s modifier) \v A vertical whitespace \V A non vertical whitespace \R A generic newline (?>\v|\x0D\x0A) \C Match a byte (with Unicode, '.' matches a character) \pP Match P-named (Unicode) property \p{...} Match Unicode property with name longer than 1 character \PP Match non-P \P{...} Match lack of Unicode property with name longer than 1 char \X Match Unicode extended grapheme cluster</code></pre> <p>POSIX character classes and their Unicode and Perl equivalents:</p> <pre><code class="plaintext">alnum IsAlnum Alphanumeric alpha IsAlpha Alphabetic ascii IsASCII Any ASCII char blank IsSpace [ \t] Horizontal whitespace (GNU extension) cntrl IsCntrl Control characters digit IsDigit \d Digits graph IsGraph Alphanumeric and punctuation lower IsLower Lowercase chars (locale and Unicode aware) print IsPrint Alphanumeric, punct, and space punct IsPunct Punctuation space IsSpace [\s\ck] Whitespace IsSpacePerl \s Perl's whitespace definition upper IsUpper Uppercase chars (locale and Unicode aware) word IsWord \w Alphanumeric plus _ (Perl extension) xdigit IsXDigit [0-9A-Fa-f] Hexadecimal digit</code></pre> <p>Within a character class:</p> <pre><code class="plaintext">POSIX traditional Unicode [:digit:] \d \p{IsDigit} [:^digit:] \D \P{IsDigit}</code></pre> <h2 id="ANCHORS"><a class="permalink" href="#ANCHORS">#</a>ANCHORS</h2> <p>All are zero-width assertions.</p> <pre><code class="plaintext">^ Match string start (or line, if /m is used) $ Match string end (or line, if /m is used) or before newline \b Match word boundary (between \w and \W) \B Match except at word boundary (between \w and \w or \W and \W) \A Match string start (regardless of /m) \Z Match string end (before optional newline) \z Match absolute string end \G Match where previous m//g left off \K Keep the stuff left of the \K, don't include it in $&</code></pre> <h2 id="QUANTIFIERS"><a class="permalink" href="#QUANTIFIERS">#</a>QUANTIFIERS</h2> <p>Quantifiers are greedy by default and match the <b>longest</b> leftmost.</p> <pre><code class="plaintext">Maximal Minimal Possessive Allowed range ------- ------- ---------- ------------- {n,m} {n,m}? {n,m}+ Must occur at least n times but no more than m times {n,} {n,}? {n,}+ Must occur at least n times {n} {n}? {n}+ Must occur exactly n times * *? *+ 0 or more times (same as {0,}) + +? ++ 1 or more times (same as {1,}) ? ?? ?+ 0 or 1 time (same as {0,1})</code></pre> <p>The possessive forms (new in Perl 5.10) prevent backtracking: what gets matched by a pattern with a possessive quantifier will not be backtracked into, even if that causes the whole match to fail.</p> <p>There is no quantifier <code>{,n}</code>. That's interpreted as a literal string.</p> <h2 id="EXTENDED-CONSTRUCTS"><a class="permalink" href="#EXTENDED-CONSTRUCTS">#</a><a id="EXTENDED"></a>EXTENDED CONSTRUCTS</h2> <pre><code class="plaintext">(?#text) A comment (?:...) Groups subexpressions without capturing (cluster) (?pimsx-imsx:...) Enable/disable option (as per m// modifiers) (?=...) Zero-width positive lookahead assertion (?!...) Zero-width negative lookahead assertion (?<=...) Zero-width positive lookbehind assertion (?<!...) Zero-width negative lookbehind assertion (?>...) Grab what we can, prohibit backtracking (?|...) Branch reset (?<name>...) Named capture (?'name'...) Named capture (?P<name>...) Named capture (python syntax) (?{ code }) Embedded code, return value becomes $^R (??{ code }) Dynamic regex, return value used as regex (?N) Recurse into subpattern number N (?-N), (?+N) Recurse into Nth previous/next subpattern (?R), (?0) Recurse at the beginning of the whole pattern (?&name) Recurse into a named subpattern (?P>name) Recurse into a named subpattern (python syntax) (?(cond)yes|no) (?(cond)yes) Conditional expression, where "cond" can be: (N) subpattern N has matched something (<name>) named subpattern has matched something ('name') named subpattern has matched something (?{code}) code condition (R) true if recursing (RN) true if recursing into Nth subpattern (R&name) true if recursing into named subpattern (DEFINE) always false, no no-pattern allowed</code></pre> <h2 id="VARIABLES"><a class="permalink" href="#VARIABLES">#</a>VARIABLES</h2> <pre><code>$_ Default variable for operators to use $` Everything prior to matched string $& Entire matched string $' Everything after to matched string ${^PREMATCH} Everything prior to matched string ${^MATCH} Entire matched string ${^POSTMATCH} Everything after to matched string</code></pre> <p>The use of <code>$`</code>, <code>$&</code> or <code>$'</code> will slow down <b>all</b> regex use within your program. Consult <a href="/5.12.1/perlvar">perlvar</a> for <code>@-</code> to see equivalent expressions that won't cause slow down. See also <a href="/5.12.1/Devel::SawAmpersand">Devel::SawAmpersand</a>. Starting with Perl 5.10, you can also use the equivalent variables <code>${^PREMATCH}</code>, <code>${^MATCH}</code> and <code>${^POSTMATCH}</code>, but for them to be defined, you have to specify the <code>/p</code> (preserve) modifier on your regular expression.</p> <pre><code>$1, $2 ... hold the Xth captured expr $+ Last parenthesized pattern match $^N Holds the most recently closed capture $^R Holds the result of the last (?{...}) expr @- Offsets of starts of groups. $-[0] holds start of whole match @+ Offsets of ends of groups. $+[0] holds end of whole match %+ Named capture buffers %- Named capture buffers, as array refs</code></pre> <p>Captured groups are numbered according to their <i>opening</i> paren.</p> <h2 id="FUNCTIONS"><a class="permalink" href="#FUNCTIONS">#</a>FUNCTIONS</h2> <pre><code class="plaintext">lc Lowercase a string lcfirst Lowercase first char of a string uc Uppercase a string ucfirst Titlecase first char of a string pos Return or set current match position quotemeta Quote metacharacters reset Reset ?pattern? status study Analyze string for optimizing matching split Use a regex to split a string into parts</code></pre> <p>The first four of these are like the escape sequences <code>\L</code>, <code>\l</code>, <code>\U</code>, and <code>\u</code>. For Titlecase, see <a href="#Titlecase">"Titlecase"</a>.</p> <h2 id="TERMINOLOGY"><a class="permalink" href="#TERMINOLOGY">#</a>TERMINOLOGY</h2> <h3 id="Titlecase"><a class="permalink" href="#Titlecase">#</a>Titlecase</h3> <p>Unicode concept which most often is equal to uppercase, but for certain characters like the German "sharp s" there is a difference.</p> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>Iain Truskett. Updated by the Perl 5 Porters.</p> <p>This document may be distributed under the same terms as Perl itself.</p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <ul> <li><p><a href="/5.12.1/perlretut">perlretut</a> for a tutorial on regular expressions.</p> </li> <li><p><a href="/5.12.1/perlrequick">perlrequick</a> for a rapid tutorial.</p> </li> <li><p><a href="/5.12.1/perlre">perlre</a> for more details.</p> </li> <li><p><a href="/5.12.1/perlvar">perlvar</a> for details on the variables.</p> </li> <li><p><a href="/5.12.1/perlop">perlop</a> for details on the operators.</p> </li> <li><p><a href="/5.12.1/perlfunc">perlfunc</a> for details on the functions.</p> </li> <li><p><a href="/5.12.1/perlfaq6">perlfaq6</a> for FAQs on regular expressions.</p> </li> <li><p><a href="/5.12.1/perlrebackslash">perlrebackslash</a> for a reference on backslash sequences.</p> </li> <li><p><a href="/5.12.1/perlrecharclass">perlrecharclass</a> for a reference on character classes.</p> </li> <li><p>The <a href="/5.12.1/re">re</a> module to alter behaviour and aid debugging.</p> </li> <li><p><a href="/5.12.1/perldebug#Debugging-regular-expressions">"Debugging regular expressions" in perldebug</a></p> </li> <li><p><a href="/5.12.1/perluniintro">perluniintro</a>, <a href="/5.12.1/perlunicode">perlunicode</a>, <a href="/5.12.1/charnames">charnames</a> and <a href="/5.12.1/perllocale">perllocale</a> for details on regexes and internationalisation.</p> </li> <li><p><i>Mastering Regular Expressions</i> by Jeffrey Friedl (<i>http://oreilly.com/catalog/9780596528126/</i>) for a thorough grounding and reference on the topic.</p> </li> </ul> <h1 id="THANKS"><a class="permalink" href="#THANKS">#</a>THANKS</h1> <p>David P.C. Wollmann, Richard Soderberg, Sean M. Burke, Tom Christiansen, Jim Cromie, and Jeffrey Goff for useful advice.</p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>