⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perlre.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 5 页
字号:
matches is modified somewhat, in that contents to the left of \f(CW\*(C`\eG\*(C'\fR isnot counted when determining the length of the match. Thus the followingwill not match forever:.IX Xref "\eG".PP.Vb 5\&    $str = \*(AqABC\*(Aq;\&    pos($str) = 1;\&    while (/.\eG/g) {\&        print $&;\&    }.Ve.PPIt will print 'A' and then terminate, as it considers the match tobe zero-width, and thus will not match at the same position twice in arow..PPIt is worth noting that \f(CW\*(C`\eG\*(C'\fR improperly used can result in an infiniteloop. Take care when using patterns that include \f(CW\*(C`\eG\*(C'\fR in an alternation..PP\fICapture buffers\fR.IX Subsection "Capture buffers".PPThe bracketing construct \f(CW\*(C`( ... )\*(C'\fR creates capture buffers. To referto the current contents of a buffer later on, within the same pattern,use \e1 for the first, \e2 for the second, and so on.Outside the match use \*(L"$\*(R" instead of \*(L"\e\*(R".  (The\&\e<digit> notation works in certain circumstances outsidethe match.  See the warning below about \e1 vs \f(CW$1\fR for details.)Referring back to another part of the match is called a\&\fIbackreference\fR..IX Xref "regex, capture buffer regexp, capture buffer regular expression, capture buffer backreference".PPThere is no limit to the number of captured substrings that you mayuse.  However Perl also uses \e10, \e11, etc. as aliases for \e010,\&\e011, etc.  (Recall that 0 means octal, so \e011 is the character atnumber 9 in your coded character set; which would be the 10th character,a horizontal tab under \s-1ASCII\s0.)  Perl resolves thisambiguity by interpreting \e10 as a backreference only if at least 10left parentheses have opened before it.  Likewise \e11 is abackreference only if at least 11 left parentheses have openedbefore it.  And so on.  \e1 through \e9 are always interpreted asbackreferences..PP    In order to provide a safer and easier way to construct patterns usingbackreferences, Perl provides the \f(CW\*(C`\eg{N}\*(C'\fR notation (starting with perl5.10.0). The curly brackets are optional, however omitting them is lesssafe as the meaning of the pattern can be changed by text (such as digits)following it. When N is a positive integer the \f(CW\*(C`\eg{N}\*(C'\fR notation isexactly equivalent to using normal backreferences. When N is a negativeinteger then it is a relative backreference referring to the previous N'thcapturing group. When the bracket form is used and N is not an integer, itis treated as a reference to a named buffer..IX Xref "\eg{1} \eg{-1} \eg{name} relative backreference named backreference".PPThus \f(CW\*(C`\eg{\-1}\*(C'\fR refers to the last buffer, \f(CW\*(C`\eg{\-2}\*(C'\fR refers to thebuffer before that. For example:.PP.Vb 8\&        /\&         (Y)            # buffer 1\&         (              # buffer 2\&            (X)         # buffer 3\&            \eg{\-1}      # backref to buffer 3\&            \eg{\-3}      # backref to buffer 1\&         )\&        /x.Ve.PPand would match the same as \f(CW\*(C`/(Y) ( (X) \e3 \e1 )/x\*(C'\fR..PPAdditionally, as of Perl 5.10.0 you may use named capture buffers and namedbackreferences. The notation is \f(CW\*(C`(?<name>...)\*(C'\fR to declare and \f(CW\*(C`\ek<name>\*(C'\fRto reference. You may also use apostrophes instead of angle brackets to delimit thename; and you may use the bracketed \f(CW\*(C`\eg{name}\*(C'\fR backreference syntax.It's possible to refer to a named capture buffer by absolute and relative number as well.Outside the pattern, a named capture buffer is available via the \f(CW\*(C`%+\*(C'\fR hash.When different buffers within the same pattern have the same name, \f(CW$+{name}\fRand \f(CW\*(C`\ek<name>\*(C'\fR refer to the leftmost defined group. (Thus it's possibleto do things with named capture buffers that would otherwise require \f(CW\*(C`(??{})\*(C'\fRcode to accomplish.).IX Xref "named capture buffer regular expression, named capture buffer %+ $+{name} \ek<name>".PPExamples:.PP.Vb 1\&    s/^([^ ]*) *([^ ]*)/$2 $1/;     # swap first two words\&\&    /(.)\e1/                         # find first doubled char\&         and print "\*(Aq$1\*(Aq is the first doubled character\en";\&\&    /(?<char>.)\ek<char>/            # ... a different way\&         and print "\*(Aq$+{char}\*(Aq is the first doubled character\en";\&\&    /(?\*(Aqchar\*(Aq.)\e1/                  # ... mix and match\&         and print "\*(Aq$1\*(Aq is the first doubled character\en";\&\&    if (/Time: (..):(..):(..)/) {   # parse out values\&        $hours = $1;\&        $minutes = $2;\&        $seconds = $3;\&    }.Ve.PPSeveral special variables also refer back to portions of the previousmatch.  \f(CW$+\fR returns whatever the last bracket match matched.\&\f(CW$&\fR returns the entire matched string.  (At one point \f(CW$0\fR didalso, but now it returns the name of the program.)  \f(CW\*(C`$\`\*(C'\fR returnseverything before the matched string.  \f(CW\*(C`$\*(Aq\*(C'\fR returns everythingafter the matched string. And \f(CW$^N\fR contains whatever was matched bythe most-recently closed group (submatch). \f(CW$^N\fR can be used inextended patterns (see below), for example to assign a submatch to avariable..IX Xref "$+ $^N $& $` $'".PPThe numbered match variables ($1, \f(CW$2\fR, \f(CW$3\fR, etc.) and the related punctuationset (\f(CW$+\fR, \f(CW$&\fR, \f(CW\*(C`$\`\*(C'\fR, \f(CW\*(C`$\*(Aq\*(C'\fR, and \f(CW$^N\fR) are all dynamically scopeduntil the end of the enclosing block or until the next successfulmatch, whichever comes first.  (See \*(L"Compound Statements\*(R" in perlsyn.).IX Xref "$+ $^N $& $` $' $1 $2 $3 $4 $5 $6 $7 $8 $9".PP\&\fB\s-1NOTE\s0\fR: Failed matches in Perl do not reset the match variables,which makes it easier to write code that tests for a series of morespecific cases and remembers the best match..PP\&\fB\s-1WARNING\s0\fR: Once Perl sees that you need one of \f(CW$&\fR, \f(CW\*(C`$\`\*(C'\fR, or\&\f(CW\*(C`$\*(Aq\*(C'\fR anywhere in the program, it has to provide them for everypattern match.  This may substantially slow your program.  Perluses the same mechanism to produce \f(CW$1\fR, \f(CW$2\fR, etc, so you also pay aprice for each pattern that contains capturing parentheses.  (Toavoid this cost while retaining the grouping behaviour, use theextended regular expression \f(CW\*(C`(?: ... )\*(C'\fR instead.)  But if you neveruse \f(CW$&\fR, \f(CW\*(C`$\`\*(C'\fR or \f(CW\*(C`$\*(Aq\*(C'\fR, then patterns \fIwithout\fR capturingparentheses will not be penalized.  So avoid \f(CW$&\fR, \f(CW\*(C`$\*(Aq\*(C'\fR, and \f(CW\*(C`$\`\*(C'\fRif you can, but if you can't (and some algorithms really appreciatethem), once you've used them once, use them at will, because you'vealready paid the price.  As of 5.005, \f(CW$&\fR is not so costly as theother two..IX Xref "$& $` $'".PPAs a workaround for this problem, Perl 5.10.0 introduces \f(CW\*(C`${^PREMATCH}\*(C'\fR,\&\f(CW\*(C`${^MATCH}\*(C'\fR and \f(CW\*(C`${^POSTMATCH}\*(C'\fR, which are equivalent to \f(CW\*(C`$\`\*(C'\fR, \f(CW$&\fRand \f(CW\*(C`$\*(Aq\*(C'\fR, \fBexcept\fR that they are only guaranteed to be defined after asuccessful match that was executed with the \f(CW\*(C`/p\*(C'\fR (preserve) modifier.The use of these variables incurs no global performance penalty, unliketheir punctuation char equivalents, however at the trade-off that youhave to tell perl when you want to use them..IX Xref " p p modifier".PPBackslashed metacharacters in Perl are alphanumeric, such as \f(CW\*(C`\eb\*(C'\fR,\&\f(CW\*(C`\ew\*(C'\fR, \f(CW\*(C`\en\*(C'\fR.  Unlike some other regular expression languages, thereare no backslashed symbols that aren't alphanumeric.  So anythingthat looks like \e\e, \e(, \e), \e<, \e>, \e{, or \e} is alwaysinterpreted as a literal character, not a metacharacter.  This wasonce used in a common idiom to disable or quote the special meaningsof regular expression metacharacters in a string that you want touse for a pattern. Simply quote all non\-\*(L"word\*(R" characters:.PP.Vb 1\&    $pattern =~ s/(\eW)/\e\e$1/g;.Ve.PP(If \f(CW\*(C`use locale\*(C'\fR is set, then this depends on the current locale.)Today it is more common to use the \fIquotemeta()\fR function or the \f(CW\*(C`\eQ\*(C'\fRmetaquoting escape sequence to disable all metacharacters' specialmeanings like this:.PP.Vb 1\&    /$unquoted\eQ$quoted\eE$unquoted/.Ve.PPBeware that if you put literal backslashes (those not insideinterpolated variables) between \f(CW\*(C`\eQ\*(C'\fR and \f(CW\*(C`\eE\*(C'\fR, double-quotishbackslash interpolation may lead to confusing results.  If you\&\fIneed\fR to use literal backslashes within \f(CW\*(C`\eQ...\eE\*(C'\fR,consult \*(L"Gory details of parsing quoted constructs\*(R" in perlop..Sh "Extended Patterns".IX Subsection "Extended Patterns"Perl also defines a consistent extension syntax for features notfound in standard tools like \fBawk\fR and \fBlex\fR.  The syntax is apair of parentheses with a question mark as the first thing withinthe parentheses.  The character after the question mark indicatesthe extension..PPThe stability of these extensions varies widely.  Some have beenpart of the core language for many years.  Others are experimentaland may change without warning or be completely removed.  Checkthe documentation on an individual feature to verify its currentstatus..PPA question mark was chosen for this and for the minimal-matchingconstruct because 1) question marks are rare in older regularexpressions, and 2) whenever you see one, you should stop and\&\*(L"question\*(R" exactly what is going on.  That's psychology....ie n .IP """(?#text)""" 10.el .IP "\f(CW(?#text)\fR" 10.IX Xref "(?#)".IX Item "(?#text)"A comment.  The text is ignored.  If the \f(CW\*(C`/x\*(C'\fR modifier enableswhitespace formatting, a simple \f(CW\*(C`#\*(C'\fR will suffice.  Note that Perl closesthe comment as soon as it sees a \f(CW\*(C`)\*(C'\fR, so there is no way to put a literal\&\f(CW\*(C`)\*(C'\fR in the comment..ie n .IP """(?pimsx\-imsx)""" 10.el .IP "\f(CW(?pimsx\-imsx)\fR" 10.IX Xref "(?)".IX Item "(?pimsx-imsx)"One or more embedded pattern-match modifiers, to be turned on (orturned off, if preceded by \f(CW\*(C`\-\*(C'\fR) for the remainder of the pattern orthe remainder of the enclosing pattern group (if any). This isparticularly useful for dynamic patterns, such as those read in from aconfiguration file, taken from an argument, or specified in a tablesomewhere.  Consider the case where some patterns want to be casesensitive and some do not:  The case insensitive ones merely need toinclude \f(CW\*(C`(?i)\*(C'\fR at the front of the pattern.  For example:.Sp.Vb 2\&    $pattern = "foobar";\&    if ( /$pattern/i ) { }\&\&    # more flexible:\&\&    $pattern = "(?i)foobar";\&    if ( /$pattern/ ) { }.Ve.SpThese modifiers are restored at the end of the enclosing group. For example,.Sp.Vb 1\&    ( (?i) blah ) \es+ \e1.Ve.Spwill match \f(CW\*(C`blah\*(C'\fR in any case, some spaces, and an exact (\fIincluding the case\fR!)repetition of the previous word, assuming the \f(CW\*(C`/x\*(C'\fR modifier, and no \f(CW\*(C`/i\*(C'\fRmodifier outside this group..SpNote that the \f(CW\*(C`p\*(C'\fR modifier is special in that it can only be enabled,not disabled, and that its presence anywhere in a pattern has a globaleffect. Thus \f(CW\*(C`(?\-p)\*(C'\fR and \f(CW\*(C`(?\-p:...)\*(C'\fR are meaningless and will warnwhen executed under \f(CW\*(C`use warnings\*(C'\fR..ie n .IP """(?:pattern)""" 10.el .IP "\f(CW(?:pattern)\fR" 10.IX Xref "(?:)".IX Item "(?:pattern)".PD 0.ie n .IP """(?imsx\-imsx:pattern)""" 10.el .IP "\f(CW(?imsx\-imsx:pattern)\fR" 10.IX Item "(?imsx-imsx:pattern)".PDThis is for clustering, not capturing; it groups subexpressions like\&\*(L"()\*(R", but doesn't make backreferences as \*(L"()\*(R" does.  So.Sp.Vb 1\&    @fields = split(/\eb(?:a|b|c)\eb/).Ve.Spis like.Sp.Vb 1\&    @fields = split(/\eb(a|b|c)\eb/).Ve.Spbut doesn't spit out extra fields.  It's also cheaper not to capturecharacters if you don't need to..SpAny letters between \f(CW\*(C`?\*(C'\fR and \f(CW\*(C`:\*(C'\fR act as flags modifiers as with\&\f(CW\*(C`(?imsx\-imsx)\*(C'\fR.  For example,.Sp.Vb 1\&    /(?s\-i:more.*than).*million/i.Ve.Spis equivalent to the more verbose.Sp.Vb 1\&    /(?:(?s\-i)more.*than).*million/i.Ve.ie n .IP """(?|pattern)""" 10.el .IP "\f(CW(?|pattern)\fR" 10.IX Xref "(?|) Branch reset".IX Item "(?|pattern)"This is the \*(L"branch reset\*(R" pattern, which has the special propertythat the capture buffers are numbered from the same starting pointin each alternation branch. It is available starting from perl 5.10.0..SpCapture buffers are numbered from left to right, but inside thisconstruct the numbering is restarted for each branch..SpThe numbering within each branch will be as normal, and any buffersfollowing this construct will be numbered as though the constructcontained only one branch, that being the one with the most capturebuffers in it..SpThis construct will be useful when you want to capture one of anumber of alternative matches..SpConsider the following pattern.  The numbers underneath show inwhich buffer the captured content will be stored..Sp.Vb 3\&    # before  \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-branch\-reset\-\-\-\-\-\-\-\-\-\-\- after        \&    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x\&    # 1            2         2  3        2     3     4.Ve.SpNote: as of Perl 5.10.0, branch resets interfere with the contents ofthe \f(CW\*(C`%+\*(C'\fR hash, that holds named captures. Consider using \f(CW\*(C`%\-\*(C'\fR instead..IP "Look-Around Assertions" 10

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -