.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05) .\" .\" Standard preamble: .\" ======================================================================== .de Sh \" Subsection heading .br .if t .Sp .ne 5 .PP \fB\\$1\fR .PP .. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "PERLREREF 1" .TH PERLREREF 1 "2008-03-01" "perl v5.10.0" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" perlreref \- Perl Regular Expressions Reference .SH "DESCRIPTION" .IX Header "DESCRIPTION" This is a quick reference to Perl's regular expressions. For full information see perlre and perlop, as well as the \*(L"\s-1SEE\s0 \s-1ALSO\s0\*(R" section in this document. .Sh "\s-1OPERATORS\s0" .IX Subsection "OPERATORS" \&\f(CW\*(C`=~\*(C'\fR determines to which variable the regex is applied. In its absence, \f(CW$_\fR is used. .PP .Vb 1 \& $var =~ /foo/; .Ve .PP \&\f(CW\*(C`!~\*(C'\fR 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. .PP .Vb 1 \& $var !~ /foo/; .Ve .PP \&\f(CW\*(C`m/pattern/msixpogc\*(C'\fR searches a string for a pattern match, applying the given options. .PP .Vb 9 \& m Multiline mode \- ^ and $ match internal lines \& s match as a Single line \- . matches \en \& 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\*(Aqt reset pos on failed matches when using /g .Ve .PP If 'pattern' is an empty string, the last \fIsuccessfully\fR matched regex is used. Delimiters other than '/' may be used for both this operator and the following ones. The leading \f(CW\*(C`m\*(C'\fR can be omitted if the delimiter is '/'. .PP \&\f(CW\*(C`qr/pattern/msixpo\*(C'\fR lets you store a regex in a variable, or pass one around. Modifiers as for \f(CW\*(C`m//\*(C'\fR, and are stored within the regex. .PP \&\f(CW\*(C`s/pattern/replacement/msixpogce\*(C'\fR substitutes matches of \&'pattern' with 'replacement'. Modifiers as for \f(CW\*(C`m//\*(C'\fR, with one addition: .PP .Vb 1 \& e Evaluate \*(Aqreplacement\*(Aq as an expression .Ve .PP \&'e' may be specified multiple times. 'replacement' is interpreted as a double quoted string unless a single-quote (\f(CW\*(C`\*(Aq\*(C'\fR) is the delimiter. .PP \&\f(CW\*(C`?pattern?\*(C'\fR is like \f(CW\*(C`m/pattern/\*(C'\fR but matches only once. No alternate delimiters can be used. Must be reset with \fIreset()\fR. .Sh "\s-1SYNTAX\s0" .IX Subsection "SYNTAX" .Vb 10 \& \e 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 \& \e1, \e2, \e3 ... Matches the text from the Nth group \& \eg1 or \eg{1}, \eg2 ... Matches the text from the Nth group \& \eg\-1 or \eg{\-1}, \eg\-2 ... Matches the text from the Nth previous group \& \eg{name} Named backreference \& \ek Named backreference \& \ek\*(Aqname\*(Aq Named backreference \& (?P=name) Named backreference (python syntax) .Ve .Sh "\s-1ESCAPE\s0 \s-1SEQUENCES\s0" .IX Subsection "ESCAPE SEQUENCES" These work as in normal strings. .PP .Vb 11 \& \ea Alarm (beep) \& \ee Escape \& \ef Formfeed \& \en Newline \& \er Carriage return \& \et Tab \& \e037 Any octal ASCII value \& \ex7f Any hexadecimal ASCII value \& \ex{263a} A wide hexadecimal value \& \ecx Control\-x \& \eN{name} A named character \& \& \el Lowercase next character \& \eu Titlecase next character \& \eL Lowercase until \eE \& \eU Uppercase until \eE \& \eQ Disable pattern metacharacters until \eE \& \eE End modification .Ve .PP For Titlecase, see \*(L"Titlecase\*(R". .PP This one works differently from normal strings: .PP .Vb 1 \& \eb An assertion, not backspace, except in a character class .Ve .Sh "\s-1CHARACTER\s0 \s-1CLASSES\s0" .IX Subsection "CHARACTER CLASSES" .Vb 4 \& [amy] Match \*(Aqa\*(Aq, \*(Aqm\*(Aq or \*(Aqy\*(Aq \& [f\-j] Dash specifies "range" \& [f\-j\-] Dash escaped or at start or end means \*(Aqdash\*(Aq \& [^f\-j] Caret indicates "match any character _except_ these" .Ve .PP The following sequences work within or without a character class. The first six are locale aware, all are Unicode aware. See perllocale and perlunicode for details. .PP .Vb 11 \& \ed A digit \& \eD A nondigit \& \ew A word character \& \eW A non\-word character \& \es A whitespace character \& \eS A non\-whitespace character \& \eh An horizontal white space \& \eH A non horizontal white space \& \ev A vertical white space \& \eV A non vertical white space \& \eR A generic newline (?>\ev|\ex0D\ex0A) \& \& \eC Match a byte (with Unicode, \*(Aq.\*(Aq matches a character) \& \epP Match P\-named (Unicode) property \& \ep{...} Match Unicode property with long name \& \ePP Match non\-P \& \eP{...} Match lack of Unicode property with long name \& \eX Match extended Unicode combining character sequence .Ve .PP \&\s-1POSIX\s0 character classes and their Unicode and Perl equivalents: .PP .Vb 10 \& alnum IsAlnum Alphanumeric \& alpha IsAlpha Alphabetic \& ascii IsASCII Any ASCII char \& blank IsSpace [ \et] Horizontal whitespace (GNU extension) \& cntrl IsCntrl Control characters \& digit IsDigit \ed 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 [\es\eck] Whitespace \& IsSpacePerl \es Perl\*(Aqs whitespace definition \& upper IsUpper Uppercase chars (locale and Unicode aware) \& word IsWord \ew Alphanumeric plus _ (Perl extension) \& xdigit IsXDigit [0\-9A\-Fa\-f] Hexadecimal digit .Ve .PP Within a character class: .PP .Vb 3 \& POSIX traditional Unicode \& [:digit:] \ed \ep{IsDigit} \& [:^digit:] \eD \eP{IsDigit} .Ve .Sh "\s-1ANCHORS\s0" .IX Subsection "ANCHORS" All are zero-width assertions. .PP .Vb 8 \& ^ Match string start (or line, if /m is used) \& $ Match string end (or line, if /m is used) or before newline \& \eb Match word boundary (between \ew and \eW) \& \eB Match except at word boundary (between \ew and \ew or \eW and \eW) \& \eA Match string start (regardless of /m) \& \eZ Match string end (before optional newline) \& \ez Match absolute string end \& \eG Match where previous m//g left off \& \& \eK Keep the stuff left of the \eK, don\*(Aqt include it in $& .Ve .Sh "\s-1QUANTIFIERS\s0" .IX Subsection "QUANTIFIERS" Quantifiers are greedy by default \*(-- match the \fBlongest\fR leftmost. .PP .Vb 9 \& 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}) .Ve .PP 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. .PP There is no quantifier {,n} \*(-- that gets understood as a literal string. .Sh "\s-1EXTENDED\s0 \s-1CONSTRUCTS\s0" .IX Subsection "EXTENDED CONSTRUCTS" .Vb 10 \& (?#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 \& (?...) Grab what we can, prohibit backtracking \& (?|...) Branch reset \& (?...) Named capture \& (?\*(Aqname\*(Aq...) Named capture \& (?P...) 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 \& () named subpattern has matched something \& (\*(Aqname\*(Aq) 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 .Ve .Sh "\s-1VARIABLES\s0" .IX Subsection "VARIABLES" .Vb 1 \& $_ Default variable for operators to use \& \& $\` Everything prior to matched string \& $& Entire matched string \& $\*(Aq Everything after to matched string \& \& ${^PREMATCH} Everything prior to matched string \& ${^MATCH} Entire matched string \& ${^POSTMATCH} Everything after to matched string .Ve .PP The use of \f(CW\*(C`$\`\*(C'\fR, \f(CW$&\fR or \f(CW\*(C`$\*(Aq\*(C'\fR will slow down \fBall\fR regex use within your program. Consult perlvar for \f(CW\*(C`@\-\*(C'\fR to see equivalent expressions that won't cause slow down. See also Devel::SawAmpersand. Starting with Perl 5.10, you can also use the equivalent variables \f(CW\*(C`${^PREMATCH}\*(C'\fR, \f(CW\*(C`${^MATCH}\*(C'\fR and \f(CW\*(C`${^POSTMATCH}\*(C'\fR, but for them to be defined, you have to specify the \f(CW\*(C`/p\*(C'\fR (preserve) modifier on your regular expression. .PP .Vb 8 \& $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 .Ve .PP Captured groups are numbered according to their \fIopening\fR paren. .Sh "\s-1FUNCTIONS\s0" .IX Subsection "FUNCTIONS" .Vb 4 \& 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 .Ve .PP The first four of these are like the escape sequences \f(CW\*(C`\eL\*(C'\fR, \f(CW\*(C`\el\*(C'\fR, \&\f(CW\*(C`\eU\*(C'\fR, and \f(CW\*(C`\eu\*(C'\fR. For Titlecase, see \*(L"Titlecase\*(R". .Sh "\s-1TERMINOLOGY\s0" .IX Subsection "TERMINOLOGY" \fITitlecase\fR .IX Subsection "Titlecase" .PP Unicode concept which most often is equal to uppercase, but for certain characters like the German \*(L"sharp s\*(R" there is a difference. .SH "AUTHOR" .IX Header "AUTHOR" Iain Truskett. Updated by the Perl 5 Porters. .PP This document may be distributed under the same terms as Perl itself. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\(bu" 4 perlretut for a tutorial on regular expressions. .IP "\(bu" 4 perlrequick for a rapid tutorial. .IP "\(bu" 4 perlre for more details. .IP "\(bu" 4 perlvar for details on the variables. .IP "\(bu" 4 perlop for details on the operators. .IP "\(bu" 4 perlfunc for details on the functions. .IP "\(bu" 4 perlfaq6 for FAQs on regular expressions. .IP "\(bu" 4 perlrebackslash for a reference on backslash sequences. .IP "\(bu" 4 perlrecharclass for a reference on character classes. .IP "\(bu" 4 The re module to alter behaviour and aid debugging. .IP "\(bu" 4 \&\*(L"Debugging regular expressions\*(R" in perldebug .IP "\(bu" 4 perluniintro, perlunicode, charnames and perllocale for details on regexes and internationalisation. .IP "\(bu" 4 \&\fIMastering Regular Expressions\fR by Jeffrey Friedl (\fIhttp://regex.info/\fR) for a thorough grounding and reference on the topic. .SH "THANKS" .IX Header "THANKS" David P.C. Wollmann, Richard Soderberg, Sean M. Burke, Tom Christiansen, Jim Cromie, and Jeffrey Goff for useful advice.