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.8.5</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" 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 active" 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.8.5/perl">Perl</a> <a class="dropdown-item" href="/5.8.5/perlintro">Intro</a> <a class="dropdown-item" href="/5.8.5/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.8.5/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.8.5/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.5/perlop">Operators</a> <a class="dropdown-item" href="/5.8.5/functions">Functions</a> <a class="dropdown-item" href="/5.8.5/variables">Variables</a> <a class="dropdown-item" href="/5.8.5/modules">Modules</a> <a class="dropdown-item" href="/5.8.5/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.5/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.8.5/perlcommunity">Community</a> <a class="dropdown-item" href="/5.8.5/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.8.5/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.8.5/perlreref">perlreref</a> <div id="more"> (<a href="/5.8.5/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.8.5. <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&#39;s regular expressions. For full information see <a href="/5.8.5/perlre">perlre</a> and <a href="/5.8.5/perlop">perlop</a>, as well as the <a href="#SEE-ALSO">&quot;SEE ALSO&quot;</a> section in this document.</p> <h2 id="OPERATORS"><a class="permalink" href="#OPERATORS">#</a>OPERATORS</h2> <pre><code>=~ determines to which variable the regex is applied. In its absence, $_ is used. $var =~ /foo/; !~ 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. $var !~ /foo/; m/pattern/igmsoxc searches a string for a pattern match, applying the given options. i case-Insensitive g Global - all occurrences m Multiline mode - ^ and $ match internal lines s match as a Single line - . matches \n o compile pattern Once x eXtended legibility - free whitespace and comments c don&#39;t reset pos on failed matches when using /g If &#39;pattern&#39; is an empty string, the last I&lt;successfully&gt; matched regex is used. Delimiters other than &#39;/&#39; may be used for both this operator and the following ones. qr/pattern/imsox lets you store a regex in a variable, or pass one around. Modifiers as for m// and are stored within the regex. s/pattern/replacement/igmsoxe substitutes matches of &#39;pattern&#39; with &#39;replacement&#39;. Modifiers as for m// with one addition: e Evaluate replacement as an expression &#39;e&#39; may be specified multiple times. &#39;replacement&#39; is interpreted as a double quoted string unless a single-quote (&#39;) is the delimiter. ?pattern? is like m/pattern/ but matches only once. No alternate delimiters can be used. Must be reset with L&lt;reset|perlfunc/reset&gt;.</code></pre> <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 ... The text from the Nth group</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 \038 Any octal ASCII value \x7f Any hexadecimal ASCII value \x{263a} A wide hexadecimal value \cx Control-x \N{name} A named character \l Lowercase next character \u Titlecase next character \L Lowercase until \E \U Uppercase until \E \Q Disable pattern metacharacters until \E \E End case modification</code></pre> <p>For Titlecase, see <a href="#Titlecase">&quot;Titlecase&quot;</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 &#39;a&#39;, &#39;m&#39; or &#39;y&#39; [f-j] Dash specifies &quot;range&quot; [f-j-] Dash escaped or at start or end means &#39;dash&#39; [^f-j] Caret indicates &quot;match any character _except_ these&quot;</code></pre> <p>The following sequences work within or without a character class. The first six are locale aware, all are Unicode aware. The default character class equivalent are given. See <a href="/5.8.5/perllocale">perllocale</a> and <a href="/5.8.5/perlunicode">perlunicode</a> for details.</p> <pre><code class="plaintext">\d A digit [0-9] \D A nondigit [^0-9] \w A word character [a-zA-Z0-9_] \W A non-word character [^a-zA-Z0-9_] \s A whitespace character [ \t\n\r\f] \S A non-whitespace character [^ \t\n\r\f] \C Match a byte (with Unicode, &#39;.&#39; matches a character) \pP Match P-named (Unicode) property \p{...} Match Unicode property with long name \PP Match non-P \P{...} Match lack of Unicode property with long name \X Match extended unicode sequence</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&#39;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</code></pre> <h2 id="QUANTIFIERS"><a class="permalink" href="#QUANTIFIERS">#</a>QUANTIFIERS</h2> <p>Quantifiers are greedy by default -- match the <b>longest</b> leftmost.</p> <pre><code class="plaintext">Maximal Minimal Allowed range ------- ------- ------------- {n,m} {n,m}? Must occur at least n times but no more than m times {n,} {n,}? Must occur at least n times {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>There is no quantifier {,n} -- that gets understood 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 (?imxs-imsx:...) Enable/disable option (as per m// modifiers) (?=...) Zero-width positive lookahead assertion (?!...) Zero-width negative lookahead assertion (?&lt;=...) Zero-width positive lookbehind assertion (?&lt;!...) Zero-width negative lookbehind assertion (?&gt;...) Grab what we can, prohibit backtracking (?{ code }) Embedded code, return value becomes $^R (??{ code }) Dynamic regex, return value used as regex (?(cond)yes|no) cond being integer corresponding to capturing parens (?(cond)yes) or a lookaround/eval zero-width assertion</code></pre> <h2 id="VARIABLES"><a class="permalink" href="#VARIABLES">#</a>VARIABLES</h2> <pre><code>$_ Default variable for operators to use $* Enable multiline matching (deprecated; not in 5.9.0 or later) $&amp; Entire matched string $` Everything prior to matched string $&#39; Everything after to matched string</code></pre> <p>The use of those last three will slow down <b>all</b> regex use within your program. Consult <a href="/5.8.5/perlvar">perlvar</a> for <code>@LAST_MATCH_START</code> to see equivalent expressions that won&#39;t cause slow down. See also <a href="/5.8.5/Devel::SawAmpersand">Devel::SawAmpersand</a>.</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</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 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">&quot;Titlecase&quot;</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 &quot;sharp s&quot; there is a difference.</p> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>Iain Truskett.</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.8.5/perlretut">perlretut</a> for a tutorial on regular expressions.</p> </li> <li><p><a href="/5.8.5/perlrequick">perlrequick</a> for a rapid tutorial.</p> </li> <li><p><a href="/5.8.5/perlre">perlre</a> for more details.</p> </li> <li><p><a href="/5.8.5/perlvar">perlvar</a> for details on the variables.</p> </li> <li><p><a href="/5.8.5/perlop">perlop</a> for details on the operators.</p> </li> <li><p><a href="/5.8.5/perlfunc">perlfunc</a> for details on the functions.</p> </li> <li><p><a href="/5.8.5/perlfaq6">perlfaq6</a> for FAQs on regular expressions.</p> </li> <li><p>The <a href="/5.8.5/re">re</a> module to alter behaviour and aid debugging.</p> </li> <li><p><a href="/5.8.5/perldebug#Debugging-regular-expressions">&quot;Debugging regular expressions&quot; in perldebug</a></p> </li> <li><p><a href="/5.8.5/perluniintro">perluniintro</a>, <a href="/5.8.5/perlunicode">perlunicode</a>, <a href="/5.8.5/charnames">charnames</a> and <a href="/5.8.5/locale">locale</a> for details on regexes and internationalisation.</p> </li> <li><p><i>Mastering Regular Expressions</i> by Jeffrey Friedl (<i>http://regex.info/</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>

Pages: 1 2 3 4 5 6 7 8 9 10