perlre.pod
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· POD 代码 · 共 1,737 行 · 第 1/5 页
POD
1,737 行
=head1 NAMEX<regular expression> X<regex> X<regexp>perlre - Perl regular expressions=head1 DESCRIPTIONThis page describes the syntax of regular expressions in Perl.If you haven't used regular expressions before, a quick-startintroduction is available in L<perlrequick>, and a longer tutorialintroduction is available in L<perlretut>.For reference on how regular expressions are used in matchingoperations, plus various examples of the same, see discussions ofC<m//>, C<s///>, C<qr//> and C<??> in L<perlop/"Regexp Quote-LikeOperators">.=head2 ModifiersMatching operations can have various modifiers. Modifiersthat relate to the interpretation of the regular expression insideare listed below. Modifiers that alter the way a regular expressionis used by Perl are detailed in L<perlop/"Regexp Quote-Like Operators"> andL<perlop/"Gory details of parsing quoted constructs">.=over 4=item mX</m> X<regex, multiline> X<regexp, multiline> X<regular expression, multiline>Treat string as multiple lines. That is, change "^" and "$" from matchingthe start or end of the string to matching the start or end of anyline anywhere within the string.=item sX</s> X<regex, single-line> X<regexp, single-line>X<regular expression, single-line>Treat string as single line. That is, change "." to match any characterwhatsoever, even a newline, which normally it would not match.Used together, as /ms, they let the "." match any character whatsoever,while still allowing "^" and "$" to match, respectively, just afterand just before newlines within the string.=item iX</i> X<regex, case-insensitive> X<regexp, case-insensitive>X<regular expression, case-insensitive>Do case-insensitive pattern matching.If C<use locale> is in effect, the case map is taken from the currentlocale. See L<perllocale>.=item xX</x>Extend your pattern's legibility by permitting whitespace and comments.=item pX</p> X<regex, preserve> X<regexp, preserve>Preserve the string matched such that ${^PREMATCH}, {$^MATCH}, and${^POSTMATCH} are available for use after matching.=item g and cX</g> X</c>Global matching, and keep the Current position after failed matching.Unlike i, m, s and x, these two flags affect the way the regex is usedrather than the regex itself. SeeL<perlretut/"Using regular expressions in Perl"> for further explanationof the g and c modifiers.=backThese are usually written as "the C</x> modifier", even though the delimiterin question might not really be a slash. Any of thesemodifiers may also be embedded within the regular expression itself usingthe C<(?...)> construct. See below.The C</x> modifier itself needs a little more explanation. It tellsthe regular expression parser to ignore whitespace that is neitherbackslashed nor within a character class. You can use this to break upyour regular expression into (slightly) more readable parts. The C<#>character is also treated as a metacharacter introducing a comment,just as in ordinary Perl code. This also means that if you want realwhitespace or C<#> characters in the pattern (outside a characterclass, where they are unaffected by C</x>), then you'll either have toescape them (using backslashes or C<\Q...\E>) or encode them using octalor hex escapes. Taken together, these features go a long way towardsmaking Perl's regular expressions more readable. Note that you have tobe careful not to include the pattern delimiter in the comment--perl hasno way of knowing you did not intend to close the pattern early. Seethe C-comment deletion code in L<perlop>. Also note that anything insidea C<\Q...\E> stays unaffected by C</x>.X</x>=head2 Regular Expressions=head3 MetacharactersThe patterns used in Perl pattern matching evolved from the ones supplied inthe Version 8 regex routines. (The routines are derived(distantly) from Henry Spencer's freely redistributable reimplementationof the V8 routines.) See L<Version 8 Regular Expressions> fordetails.In particular the following metacharacters have their standard I<egrep>-ishmeanings:X<metacharacter>X<\> X<^> X<.> X<$> X<|> X<(> X<()> X<[> X<[]> \ Quote the next metacharacter ^ Match the beginning of the line . Match any character (except newline) $ Match the end of the line (or before newline at the end) | Alternation () Grouping [] Character classBy default, the "^" character is guaranteed to match only thebeginning of the string, the "$" character only the end (or before thenewline at the end), and Perl does certain optimizations with theassumption that the string contains only one line. Embedded newlineswill not be matched by "^" or "$". You may, however, wish to treat astring as a multi-line buffer, such that the "^" will match after anynewline within the string (except if the newline is the last character inthe string), and "$" will match before any newline. At thecost of a little more overhead, you can do this by using the /m modifieron the pattern match operator. (Older programs did this by setting C<$*>,but this practice has been removed in perl 5.9.)X<^> X<$> X</m>To simplify multi-line substitutions, the "." character never matches anewline unless you use the C</s> modifier, which in effect tells Perl to pretendthe string is a single line--even if it isn't.X<.> X</s>=head3 QuantifiersThe following standard quantifiers are recognized:X<metacharacter> X<quantifier> X<*> X<+> X<?> X<{n}> X<{n,}> X<{n,m}> * Match 0 or more times + Match 1 or more times ? Match 1 or 0 times {n} Match exactly n times {n,} Match at least n times {n,m} Match at least n but not more than m times(If a curly bracket occurs in any other context, it is treatedas a regular character. In particular, the lower boundis not optional.) The "*" quantifier is equivalent to C<{0,}>, the "+"quantifier to C<{1,}>, and the "?" quantifier to C<{0,1}>. n and m are limitedto integral values less than a preset limit defined when perl is built.This is usually 32766 on the most common platforms. The actual limit canbe seen in the error message generated by code such as this: $_ **= $_ , / {$_} / for 2 .. 42;By default, a quantified subpattern is "greedy", that is, it will match asmany times as possible (given a particular starting location) while stillallowing the rest of the pattern to match. If you want it to match theminimum number of times possible, follow the quantifier with a "?". Notethat the meanings don't change, just the "greediness":X<metacharacter> X<greedy> X<greediness>X<?> X<*?> X<+?> X<??> X<{n}?> X<{n,}?> X<{n,m}?> *? Match 0 or more times, not greedily +? Match 1 or more times, not greedily ?? Match 0 or 1 time, not greedily {n}? Match exactly n times, not greedily {n,}? Match at least n times, not greedily {n,m}? Match at least n but not more than m times, not greedilyBy default, when a quantified subpattern does not allow the rest of theoverall pattern to match, Perl will backtrack. However, this behaviour issometimes undesirable. Thus Perl provides the "possessive" quantifier formas well. *+ Match 0 or more times and give nothing back ++ Match 1 or more times and give nothing back ?+ Match 0 or 1 time and give nothing back {n}+ Match exactly n times and give nothing back (redundant) {n,}+ Match at least n times and give nothing back {n,m}+ Match at least n but not more than m times and give nothing backFor instance, 'aaaa' =~ /a++a/will never match, as the C<a++> will gobble up all the C<a>'s in thestring and won't leave any for the remaining part of the pattern. Thisfeature can be extremely useful to give perl hints about where itshouldn't backtrack. For instance, the typical "match a double-quotedstring" problem can be most efficiently performed when written as: /"(?:[^"\\]++|\\.)*+"/as we know that if the final quote does not match, backtracking will nothelp. See the independent subexpression C<< (?>...) >> for more details;possessive quantifiers are just syntactic sugar for that construct. Forinstance the above example could also be written as follows: /"(?>(?:(?>[^"\\]+)|\\.)*)"/=head3 Escape sequencesBecause patterns are processed as double quoted strings, the followingalso work:X<\t> X<\n> X<\r> X<\f> X<\e> X<\a> X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q>X<\0> X<\c> X<\N> X<\x> \t tab (HT, TAB) \n newline (LF, NL) \r return (CR) \f form feed (FF) \a alarm (bell) (BEL) \e escape (think troff) (ESC) \033 octal char (example: ESC) \x1B hex char (example: ESC) \x{263a} long hex char (example: Unicode SMILEY) \cK control char (example: VT) \N{name} named Unicode character \l lowercase next char (think vi) \u uppercase next char (think vi) \L lowercase till \E (think vi) \U uppercase till \E (think vi) \E end case modification (think vi) \Q quote (disable) pattern metacharacters till \EIf C<use locale> is in effect, the case map used by C<\l>, C<\L>, C<\u>and C<\U> is taken from the current locale. See L<perllocale>. Fordocumentation of C<\N{name}>, see L<charnames>.You cannot include a literal C<$> or C<@> within a C<\Q> sequence.An unescaped C<$> or C<@> interpolates the corresponding variable,while escaping will cause the literal string C<\$> to be matched.You'll need to write something like C<m/\Quser\E\@\Qhost/>.=head3 Character Classes and other Special EscapesIn addition, Perl defines the following:X<\w> X<\W> X<\s> X<\S> X<\d> X<\D> X<\X> X<\p> X<\P> X<\C>X<\g> X<\k> X<\N> X<\K> X<\v> X<\V> X<\h> X<\H>X<word> X<whitespace> X<character class> X<backreference> \w Match a "word" character (alphanumeric plus "_") \W Match a non-"word" character \s Match a whitespace character \S Match a non-whitespace character \d Match a digit character \D Match a non-digit character \pP Match P, named property. Use \p{Prop} for longer names. \PP Match non-P \X Match eXtended Unicode "combining character sequence", equivalent to (?:\PM\pM*) \C Match a single C char (octet) even under Unicode. NOTE: breaks up characters into their UTF-8 bytes, so you may end up with malformed pieces of UTF-8. Unsupported in lookbehind. \1 Backreference to a specific group. '1' may actually be any positive integer. \g1 Backreference to a specific or previous group, \g{-1} number may be negative indicating a previous buffer and may optionally be wrapped in curly brackets for safer parsing. \g{name} Named backreference \k<name> Named backreference \K Keep the stuff left of the \K, don't include it in $& \v Vertical whitespace \V Not vertical whitespace \h Horizontal whitespace \H Not horizontal whitespace \R LinebreakA C<\w> matches a single alphanumeric character (an alphabeticcharacter, or a decimal digit) or C<_>, not a whole word. Use C<\w+>to match a string of Perl-identifier characters (which isn't the sameas matching an English word). If C<use locale> is in effect, the listof alphabetic characters generated by C<\w> is taken from the currentlocale. See L<perllocale>. You may use C<\w>, C<\W>, C<\s>, C<\S>,C<\d>, and C<\D> within character classes, but they aren't usableas either end of a range. If any of them precedes or follows a "-",the "-" is understood literally. If Unicode is in effect, C<\s> matchesalso "\x{85}", "\x{2028}", and "\x{2029}". See L<perlunicode> for moredetails about C<\pP>, C<\PP>, C<\X> and the possibility of definingyour own C<\p> and C<\P> properties, and L<perluniintro> about Unicodein general.X<\w> X<\W> X<word>C<\R> will atomically match a linebreak, including the network line-ending"\x0D\x0A". Specifically, X<\R> is exactly equivalent to (?>\x0D\x0A?|[\x0A-\x0C\x85\x{2028}\x{2029}])B<Note:> C<\R> has no special meaning inside of a character class;use C<\v> instead (vertical whitespace).X<\R>The POSIX character class syntaxX<character class> [:class:]is also available. Note that the C<[> and C<]> brackets are I<literal>;they must always be used within a character class expression. # this is correct: $string =~ /[[:alpha:]]/; # this is not, and will generate a warning: $string =~ /[:alpha:]/;The available classes and their backslash equivalents (if available) areas follows:X<character class>X<alpha> X<alnum> X<ascii> X<blank> X<cntrl> X<digit> X<graph>X<lower> X<print> X<punct> X<space> X<upper> X<word> X<xdigit> alpha alnum ascii blank [1] cntrl digit \d graph lower print punct space \s [2] upper word \w [3] xdigit=over=item [1]A GNU extension equivalent to C<[ \t]>, "all horizontal whitespace".=item [2]Not exactly equivalent to C<\s> since the C<[[:space:]]> includesalso the (very rare) "vertical tabulator", "\cK" or chr(11) in ASCII.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?