Perldoc Search: "$&" perl-5.20.1

search official POD

11 PODs, 57 LINEs found.
332 :       \K        [6]  Keep the stuff left of the \K, don't include it in $&
447 :     Several special variables also refer back to portions of the previous match. $+ returns whatever the last bracket match matched. $& returns the entire matched string. (At one point $0 did also, but now it returns the name of the program.) $` returns everything before the matched string. $' returns everything after the matched string. And $^N contains whatever was matched by the most-recently closed group (submatch). $^N can be used in extended patterns (see below), for example to assign a submatch to a variable.
453 :     WARNING: If your code is to run on Perl 5.16 or earlier, beware that once Perl sees that you need one of $&, $`, or $' anywhere in the program, it has to provide them for every pattern match. This may substantially slow your program.
455 :     Perl uses the same mechanism to produce $1, $2, etc, so you also pay a price for each pattern that contains capturing parentheses. (To avoid this cost while retaining the grouping behaviour, use the extended regular expression "(?: ... )" instead.) But if you never use $&, $` or $', then patterns *without* capturing parentheses will not be penalized. So avoid $&, $', and $` if you can, but if you can't (and some algorithms really appreciate them), once you've used them once, use them at will, because you've already paid the price.
457 :     Perl 5.16 introduced a slightly more efficient mechanism that notes separately whether each of $`, $&, and $' have been seen, and thus may only need to copy part of the string. Perl 5.20 introduced a much more efficient copy-on-write mechanism which eliminates any slowdown.
459 :     As another workaround for this problem, Perl 5.10.0 introduced "${^PREMATCH}", "${^MATCH}" and "${^POSTMATCH}", which are equivalent to $`, $& and $', except that they are only guaranteed to be defined after a successful match that was executed with the "/p" (preserve) modifier. The use of these variables incurs no global performance penalty, unlike their punctuation char equivalents, however at the trade-off that you have to tell perl when you want to use them. As of Perl 5.20, these three variables are equivalent to $`, $& and $', and "/p" is ignored.
586 :         Look-around assertions are zero-width patterns which match a specific pattern without including it in $&. Positive assertions match when their subpattern matches, negative assertions match when their subpattern fails. Look-behind matches text up to the current match position, look-ahead matches text following the current match position.
589 :             A zero-width positive look-ahead assertion. For example, "/\w+(?=\t)/" matches a word followed by a tab, without including the tab in $&.
597 :             A zero-width positive look-behind assertion. For example, "/(?<=\t)\w+/" matches a word that follows a tab, without including the tab in $&. Works only for fixed-width look-behind.
599 :             There is a special form of this construct, called "\K" (available since Perl 5.10.0), which causes the regex engine to "keep" everything it had matched prior to the "\K" and not include it in $&. This effectively provides variable-length look-behind. The use of "\K" inside of another look-around assertion is allowed, but the behaviour is currently not well defined.
962 :                'aaab' =~ /a+b?(?{print "$&\n"; $count++})(*FAIL)/;
980 :                'aaab' =~ /a+b?(*PRUNE)(?{print "$&\n"; $count++})(*FAIL)/;
1001 :             'aaabaaab' =~ /a+b?(*SKIP)(?{print "$&\n"; $count++})(*FAIL)/;
1057 :             'aaabaaab' =~ /a+b?(*COMMIT)(?{print "$&\n"; $count++})(*FAIL)/;
1258 :         print "match: <$&>\n" while 'foo' =~ m{ o? }xg;
1289 :        print $&;
1305 :         s/\w??/<$&>/g;
404 :     Traditionally in Perl, any use of any of the three variables $`, $& or $' (or their "use English" equivalents) anywhere in the code, caused all subsequent successful pattern matches to make a copy of the matched string, in case the code might subsequently access one of those variables. This imposed a considerable performance penalty across the whole program, so generally the use of these variables has been discouraged.
410 :         print $`, $&, $'; # bad: perfomance hit
421 :         $`; $&; "abcdefgh" =~ /d/
426 :         $&; # whoops
441 :     $&      The string matched by the last successful pattern match (not counting any matches hidden within a BLOCK or "eval()" enclosed by the current BLOCK).
450 :             This is similar to $& ($MATCH) except that it does not incur the performance penalty associated with that variable.
485 :                 print "$`:$&:$'\n";         # prints abc:def:ghi
551 :             Thus, after a match against $_, $& coincides with "substr $_, $-[0], $+[0] - $-[0]". Similarly, $*n* coincides with "substr $_, $-[n], $+[n] - $-[n]" if $-[n] is defined, and $+ coincides with "substr $_, $-[$#-], $+[$#-] - $-[$#-]". One can use $#- to find the last matched subgroup in the last successful match. Contrast with $#+, the number of subgroups in the regular expression. Compare with "@+".
558 :             $& is the same as "substr($var, $-[0], $+[0] - $-[0])"
202 :     Called to get/set the value of $`, $', $& and their named equivalents, ${^PREMATCH}, ${^POSTMATCH} and ${^MATCH}, as well as the numbered capture groups ($1, $2, ...).
211 :         $&            RX_BUFF_IDX_FULLMATCH
386 :             I32 minlenret;  /* mininum possible number of chars in $& */
447 :     "minlenret" is the minimum length (in characters) of the string that would be found in $& after a match.
474 :     A "regexp_paren_pair" structure which defines offsets into the string being matched which correspond to the $& and $1, $2 etc. captures, the "regexp_paren_pair" struct is defined as follows:
481 :     If "->offs[num].start" or "->offs[num].end" is -1 then that capture group did not match. "->offs[0].start/end" represents $& (or "${^MATCH}" under "//p") and "->offs[paren].end" matches $$paren where $paren = 1>.
499 :     Used during the execution phase for managing search and replace patterns, and for providing the text for $&, $1 etc. "subbeg" points to a buffer (either the original string, or a copy in the case of "RX_MATCH_COPIED(rx)"), and "sublen" is the length of the buffer. The "RX_OFFS" start and end indices index into this buffer.
483 :     Even if there are no groupings in a regexp, it is still possible to find out what exactly matched in a string. If you use them, Perl will set $` to the part of the string before the match, will set $& to the part of the string that matched, and will set $' to the part of the string after the match. An example:
486 :         $x =~ /cat/;  # $` = 'the ', $& = 'cat', $' = ' caught the mouse'
487 :         $x =~ /the/;  # $` = '', $& = 'the', $' = ' cat caught the mouse'
491 :     If your code is to run on Perl versions earlier than 5.20, it is worthwhile to note that using $` and $' slows down regexp matching quite a bit, while $& slows it down to a lesser extent, because if they are used in one regexp in a program, they are generated for *all* regexps in the program. So if raw performance is a goal of your application, they should be avoided. If you need to extract the corresponding substrings, use "@-" and "@+" instead:
494 :         $& is the same as substr( $x, $-[0], $+[0]-$-[0] )
497 :     As of Perl 5.10, the "${^PREMATCH}", "${^MATCH}" and "${^POSTMATCH}" variables may be used. These are only set if the "/p" modifier is present. Consequently they do not penalize the rest of the program. In Perl 5.20, "${^PREMATCH}", "${^MATCH}" and "${^POSTMATCH}" are available whether the "/p" has been used or not (the modifier is ignored), and $`, $' and $& do not cause any speed difference.
1233 :                 s/\d+/$&*2/e;               # yields 'abc246xyz'
1234 :                 s/\d+/sprintf("%5d",$&)/e;  # yields 'abc  246xyz'
1235 :                 s/\w/$& x 2/eg;             # yields 'aabbcc  224466xxyyzz'
1238 :                 s/%(.)/$percent{$1} || $&/ge;       # expr now, so /e
1286 :                 1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
460 :         The matched-pattern variables, $&, $` (pre-match), $' (post-match), and $+ (last match) also are tainted.
499 :             $untainted_output_file = $&;
511 :             $localized_output_file = $&;
516 :     This third program fails to run because $& is tainted: it is the result of a match involving "\w" while "use locale" is in effect.
184 :        \K Keep the stuff left of the \K, don't include it in $&
242 :        $&    Entire matched string
249 :     Note to those still using Perl 5.18 or earlier: The use of $`, $& or $' will slow down all regex use within your program. Consult perlvar for "@-" 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 "${^PREMATCH}", "${^MATCH}" and "${^POSTMATCH}", but for them to be defined, you have to specify the "/p" (preserve) modifier on your regular expression. In Perl 5.20, the use of $`, $& and $' makes no speed difference.
289 :       Omitting $` $& $' support.
560 :      # Control what to keep in $&.
561 :      KEEPS           no         $& begins here.
331 :     \K  This appeared in perl 5.10.0. Anything matched left of "\K" is not included in $&, and will not be replaced if the pattern is used in a substitution. This lets you write "s/PAT1 \K PAT2/REPL/x" instead of "s/(PAT1) PAT2/${1}REPL/x" or "s/(?<=PAT1) PAT2/REPL/x".
65 :               RLENGTH   length($&)
518 :             Omitting $` $& $' support.
<< Back to Perldoc Search