📄 perlre.1
字号:
.IX Xref "look-around assertion lookaround assertion look-around lookaround".IX Item "Look-Around Assertions"Look-around assertions are zero width patterns which match a specificpattern without including it in \f(CW$&\fR. Positive assertions match whentheir subpattern matches, negative assertions match when their subpatternfails. Look-behind matches text up to the current match position,look-ahead matches text following the current match position..RS 10.ie n .IP """(?=pattern)""" 4.el .IP "\f(CW(?=pattern)\fR" 4.IX Xref "(?=) look-ahead, positive lookahead, positive".IX Item "(?=pattern)"A zero-width positive look-ahead assertion. For example, \f(CW\*(C`/\ew+(?=\et)/\*(C'\fRmatches a word followed by a tab, without including the tab in \f(CW$&\fR..ie n .IP """(?!pattern)""" 4.el .IP "\f(CW(?!pattern)\fR" 4.IX Xref "(?!) look-ahead, negative lookahead, negative".IX Item "(?!pattern)"A zero-width negative look-ahead assertion. For example \f(CW\*(C`/foo(?!bar)/\*(C'\fRmatches any occurrence of \*(L"foo\*(R" that isn't followed by \*(L"bar\*(R". Notehowever that look-ahead and look-behind are \s-1NOT\s0 the same thing. You cannotuse this for look-behind..SpIf you are looking for a \*(L"bar\*(R" that isn't preceded by a \*(L"foo\*(R", \f(CW\*(C`/(?!foo)bar/\*(C'\fRwill not do what you want. That's because the \f(CW\*(C`(?!foo)\*(C'\fR is just saying thatthe next thing cannot be \*(L"foo\*(R"\-\-and it's not, it's a \*(L"bar\*(R", so \*(L"foobar\*(R" willmatch. You would have to do something like \f(CW\*(C`/(?!foo)...bar/\*(C'\fR for that. Wesay \*(L"like\*(R" because there's the case of your \*(L"bar\*(R" not having three charactersbefore it. You could cover that this way: \f(CW\*(C`/(?:(?!foo)...|^.{0,2})bar/\*(C'\fR.Sometimes it's still easier just to say:.Sp.Vb 1\& if (/bar/ && $\` !~ /foo$/).Ve.SpFor look-behind see below..ie n .IP """(?<=pattern)""\fR \f(CW""\eK""" 4.el .IP "\f(CW(?<=pattern)\fR \f(CW\eK\fR" 4.IX Xref "(?<=) look-behind, positive lookbehind, positive \eK".IX Item "(?<=pattern) K"A zero-width positive look-behind assertion. For example, \f(CW\*(C`/(?<=\et)\ew+/\*(C'\fRmatches a word that follows a tab, without including the tab in \f(CW$&\fR.Works only for fixed-width look-behind..SpThere is a special form of this construct, called \f(CW\*(C`\eK\*(C'\fR, which causes theregex engine to \*(L"keep\*(R" everything it had matched prior to the \f(CW\*(C`\eK\*(C'\fR andnot include it in \f(CW$&\fR. This effectively provides variable lengthlook-behind. The use of \f(CW\*(C`\eK\*(C'\fR inside of another look-around assertionis allowed, but the behaviour is currently not well defined..SpFor various reasons \f(CW\*(C`\eK\*(C'\fR may be significantly more efficient than theequivalent \f(CW\*(C`(?<=...)\*(C'\fR construct, and it is especially useful insituations where you want to efficiently remove something followingsomething else in a string. For instance.Sp.Vb 1\& s/(foo)bar/$1/g;.Ve.Spcan be rewritten as the much more efficient.Sp.Vb 1\& s/foo\eKbar//g;.Ve.ie n .IP """(?<!pattern)""" 4.el .IP "\f(CW(?<!pattern)\fR" 4.IX Xref "(?<!) look-behind, negative lookbehind, negative".IX Item "(?<!pattern)"A zero-width negative look-behind assertion. For example \f(CW\*(C`/(?<!bar)foo/\*(C'\fRmatches any occurrence of \*(L"foo\*(R" that does not follow \*(L"bar\*(R". Worksonly for fixed-width look-behind..RE.RS 10.RE.ie n .IP """(?\*(AqNAME\*(Aqpattern)""" 10.el .IP "\f(CW(?\*(AqNAME\*(Aqpattern)\fR" 10.IX Item "(?NAMEpattern)".PD 0.ie n .IP """(?<NAME>pattern)""" 10.el .IP "\f(CW(?<NAME>pattern)\fR" 10.IX Xref "(?<NAME>) (?'NAME') named capture capture".IX Item "(?<NAME>pattern)".PDA named capture buffer. Identical in every respect to normal capturingparentheses \f(CW\*(C`()\*(C'\fR but for the additional fact that \f(CW\*(C`%+\*(C'\fR or \f(CW\*(C`%\-\*(C'\fR may beused after a successful match to refer to a named buffer. See \f(CW\*(C`perlvar\*(C'\fRfor more details on the \f(CW\*(C`%+\*(C'\fR and \f(CW\*(C`%\-\*(C'\fR hashes..SpIf multiple distinct capture buffers have the same name then the$+{\s-1NAME\s0} will refer to the leftmost defined buffer in the match..SpThe forms \f(CW\*(C`(?\*(AqNAME\*(Aqpattern)\*(C'\fR and \f(CW\*(C`(?<NAME>pattern)\*(C'\fR are equivalent..Sp\&\fB\s-1NOTE:\s0\fR While the notation of this construct is the same as the similarfunction in .NET regexes, the behavior is not. In Perl the buffers arenumbered sequentially regardless of being named or not. Thus in thepattern.Sp.Vb 1\& /(x)(?<foo>y)(z)/.Ve.Sp$+{foo} will be the same as \f(CW$2\fR, and \f(CW$3\fR will contain 'z' instead ofthe opposite which is what a .NET regex hacker might expect..SpCurrently \s-1NAME\s0 is restricted to simple identifiers only.In other words, it must match \f(CW\*(C`/^[_A\-Za\-z][_A\-Za\-z0\-9]*\ez/\*(C'\fR orits Unicode extension (see utf8),though it isn't extended by the locale (see perllocale)..Sp\&\fB\s-1NOTE:\s0\fR In order to make things easier for programmers with experiencewith the Python or \s-1PCRE\s0 regex engines, the pattern \f(CW\*(C`(?P<NAME>pattern)\*(C'\fRmay be used instead of \f(CW\*(C`(?<NAME>pattern)\*(C'\fR; however this form does notsupport the use of single quotes as a delimiter for the name..ie n .IP """\ek<NAME>""" 10.el .IP "\f(CW\ek<NAME>\fR" 10.IX Item "k<NAME>".PD 0.ie n .IP """\ek\*(AqNAME\*(Aq""" 10.el .IP "\f(CW\ek\*(AqNAME\*(Aq\fR" 10.IX Item "kNAME".PDNamed backreference. Similar to numeric backreferences, except thatthe group is designated by name and not number. If multiple groupshave the same name then it refers to the leftmost defined group inthe current match..SpIt is an error to refer to a name not defined by a \f(CW\*(C`(?<NAME>)\*(C'\fRearlier in the pattern..SpBoth forms are equivalent..Sp\&\fB\s-1NOTE:\s0\fR In order to make things easier for programmers with experiencewith the Python or \s-1PCRE\s0 regex engines, the pattern \f(CW\*(C`(?P=NAME)\*(C'\fRmay be used instead of \f(CW\*(C`\ek<NAME>\*(C'\fR..ie n .IP """(?{ code })""" 10.el .IP "\f(CW(?{ code })\fR" 10.IX Xref "(?{}) regex, code in regexp, code in regular expression, code in".IX Item "(?{ code })"\&\fB\s-1WARNING\s0\fR: This extended regular expression feature is consideredexperimental, and may be changed without notice. Code executed thathas side effects may not perform identically from version to versiondue to the effect of future optimisations in the regex engine..SpThis zero-width assertion evaluates any embedded Perl code. Italways succeeds, and its \f(CW\*(C`code\*(C'\fR is not interpolated. Currently,the rules to determine where the \f(CW\*(C`code\*(C'\fR ends are somewhat convoluted..SpThis feature can be used together with the special variable \f(CW$^N\fR tocapture the results of submatches in variables without having to keeptrack of the number of nested parentheses. For example:.Sp.Vb 3\& $_ = "The brown fox jumps over the lazy dog";\& /the (\eS+)(?{ $color = $^N }) (\eS+)(?{ $animal = $^N })/i;\& print "color = $color, animal = $animal\en";.Ve.SpInside the \f(CW\*(C`(?{...})\*(C'\fR block, \f(CW$_\fR refers to the string the regularexpression is matching against. You can also use \f(CW\*(C`pos()\*(C'\fR to know what isthe current position of matching within this string..SpThe \f(CW\*(C`code\*(C'\fR is properly scoped in the following sense: If the assertionis backtracked (compare \*(L"Backtracking\*(R"), all changes introduced after\&\f(CW\*(C`local\*(C'\fRization are undone, so that.Sp.Vb 10\& $_ = \*(Aqa\*(Aq x 8;\& m<\& (?{ $cnt = 0 }) # Initialize $cnt.\& (\& a\& (?{\& local $cnt = $cnt + 1; # Update $cnt, backtracking\-safe.\& })\& )*\& aaaa\& (?{ $res = $cnt }) # On success copy to non\-localized\& # location.\& >x;.Ve.Spwill set \f(CW\*(C`$res = 4\*(C'\fR. Note that after the match, \f(CW$cnt\fR returns to the globallyintroduced value, because the scopes that restrict \f(CW\*(C`local\*(C'\fR operatorsare unwound..SpThis assertion may be used as a \f(CW\*(C`(?(condition)yes\-pattern|no\-pattern)\*(C'\fRswitch. If \fInot\fR used in this way, the result of evaluation of\&\f(CW\*(C`code\*(C'\fR is put into the special variable \f(CW$^R\fR. This happensimmediately, so \f(CW$^R\fR can be used from other \f(CW\*(C`(?{ code })\*(C'\fR assertionsinside the same regular expression..SpThe assignment to \f(CW$^R\fR above is properly localized, so the oldvalue of \f(CW$^R\fR is restored if the assertion is backtracked; compare\&\*(L"Backtracking\*(R"..SpDue to an unfortunate implementation issue, the Perl code contained in theseblocks is treated as a compile time closure that can have seemingly bizarreconsequences when used with lexically scoped variables inside of subroutinesor loops. There are various workarounds for this, including simply usingglobal variables instead. If you are using this construct and strange resultsoccur then check for the use of lexically scoped variables..SpFor reasons of security, this construct is forbidden if the regularexpression involves run-time interpolation of variables, unless theperilous \f(CW\*(C`use re \*(Aqeval\*(Aq\*(C'\fR pragma has been used (see re), or thevariables contain results of \f(CW\*(C`qr//\*(C'\fR operator (see\&\*(L"qr/STRING/imosx\*(R" in perlop)..SpThis restriction is due to the wide-spread and remarkably convenientcustom of using run-time determined strings as patterns. For example:.Sp.Vb 3\& $re = <>;\& chomp $re;\& $string =~ /$re/;.Ve.SpBefore Perl knew how to execute interpolated code within a pattern,this operation was completely safe from a security point of view,although it could raise an exception from an illegal pattern. Ifyou turn on the \f(CW\*(C`use re \*(Aqeval\*(Aq\*(C'\fR, though, it is no longer secure,so you should only do so if you are also using taint checking.Better yet, use the carefully constrained evaluation within a Safecompartment. See perlsec for details about both these mechanisms..SpBecause Perl's regex engine is currently not re-entrant, interpolatedcode may not invoke the regex engine either directly with \f(CW\*(C`m//\*(C'\fR or \f(CW\*(C`s///\*(C'\fR),or indirectly with functions such as \f(CW\*(C`split\*(C'\fR..ie n .IP """(??{ code })""" 10.el .IP "\f(CW(??{ code })\fR" 10.IX Xref "(??{}) regex, postponed regexp, postponed regular expression, postponed".IX Item "(??{ code })"\&\fB\s-1WARNING\s0\fR: This extended regular expression feature is consideredexperimental, and may be changed without notice. Code executed thathas side effects may not perform identically from version to versiondue to the effect of future optimisations in the regex engine..SpThis is a \*(L"postponed\*(R" regular subexpression. The \f(CW\*(C`code\*(C'\fR is evaluatedat run time, at the moment this subexpression may match. The resultof evaluation is considered as a regular expression and matched asif it were inserted instead of this construct. Note that this meansthat the contents of capture buffers defined inside an eval'ed patternare not available outside of the pattern, and vice versa, there is noway for the inner pattern to refer to a capture buffer defined outside.Thus,.Sp.Vb 1\& (\*(Aqa\*(Aq x 100)=~/(??{\*(Aq(.)\*(Aq x 100})/.Ve.Sp\&\fBwill\fR match, it will \fBnot\fR set \f(CW$1\fR..SpThe \f(CW\*(C`code\*(C'\fR is not interpolated. As before, the rules to determinewhere the \f(CW\*(C`code\*(C'\fR ends are currently somewhat convoluted..SpThe following pattern matches a parenthesized group:.Sp.Vb 9\& $re = qr{\& \e(\& (?:\& (?> [^()]+ ) # Non\-parens without backtracking\& |\& (??{ $re }) # Group with matching parens\& )*\& \e)\& }x;.Ve.SpSee also \f(CW\*(C`(?PARNO)\*(C'\fR for a different, more efficient way to accomplishthe same task..SpBecause perl's regex engine is not currently re-entrant, delayedcode may not invoke the regex engine either directly with \f(CW\*(C`m//\*(C'\fR or \f(CW\*(C`s///\*(C'\fR),or indirectly with functions such as \f(CW\*(C`split\*(C'\fR..SpRecursing deeper than 50 times without consuming any input string willresult in a fatal error. The maximum depth is compiled into perl, sochanging it requires a custom build..ie n .IP """(?PARNO)""\fR \f(CW""(?\-PARNO)""\fR \f(CW""(?+PARNO)""\fR \f(CW""(?R)""\fR \f(CW""(?0)""" 10.el .IP "\f(CW(?PARNO)\fR \f(CW(?\-PARNO)\fR \f(CW(?+PARNO)\fR \f(CW(?R)\fR \f(CW(?0)\fR" 10.IX Xref "(?PARNO) (?1) (?R) (?0) (?-1) (?+1) (?-PARNO) (?+PARNO) regex, recursive regexp, recursive regular expression, recursive regex, relative recursion".IX Item "(?PARNO) (?-PARNO) (?+PARNO) (?R) (?0)"Similar to \f(CW\*(C`(??{ code })\*(C'\fR except it does not involve compiling any code,instead it treats the contents of a capture buffer as an independentpattern that must match at the current position. Capture bufferscontained by the pattern will have the value as determined by theoutermost recursion..Sp\&\s-1PARNO\s0 is a sequence of digits (not starting with 0) whose value reflectsthe paren-number of the capture buffer to recurse to. \f(CW\*(C`(?R)\*(C'\fR recurses tothe beginning of the whole pattern. \f(CW\*(C`(?0)\*(C'\fR is an alternate syntax for\&\f(CW\*(C`(?R)\*(C'\fR. If \s-1PARNO\s0 is preceded by a plus or minus sign then it is assumedto be relative, with negative numbers indicating preceding capture buffersand positive ones following. Thus \f(CW\*(C`(?\-1)\*(C'\fR refers to the most recentlydeclared buffer, and \f(CW\*(C`(?+1)\*(C'\fR indicates the next buffer to be declared.Note that the counting for relative recursion differs from that ofrelative backreferences, in that with recursion unclosed buffers \fBare\fRincluded..SpThe following pattern matches a function \fIfoo()\fR which may containbalanced parentheses as the argument..Sp.Vb 10\& $re = qr{ ( # paren group 1 (full function)\& foo\& ( # paren group 2 (parens)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -