请高手解释一段Perl代码
#!/usr/bin/perl
while ( <> ) { # take one input line at a time
chomp;
if (/YOUR_PATTERN_GOES_HERE/) {
print "Matched: |$` <$&> $ '|\n "; # Mystery code! See the text.
}
else {
print "No match.\n ";
}
}
这是一段模式匹配的代码。我就是不太明白print语句里面的|$` <$&> $ '|到底是啥意思。各位高手能否解释一下?
[解决办法]
$ARG
$_ - The default input and pattern-searching space.
$ <digit> - Contains the subpattern from the corresponding set of parentheses in the last pattern matched.
$MATCH
$& - The string matched by the last successful pattern match.
$PREMATCH
$` - The string preceding whatever was matched by the last successful pattern match.
$POSTMATCH
$ ' - The string following whatever was matched by the last successful pattern match.
$LAST_PAREN_MATCH
$+ - The last bracket matched by the last search pattern.
$MULTILINE_MATCHING
$* - Set to 1 to do multi-line matching within a string, 0 to tell Perl that it can assume that strings contain a single line, for the purpose of optimizing pattern matches. Default is 0. Use of ``$* ' ' is deprecated in modern perls.
input_line_number HANDLE EXPR
$INPUT_LINE_NUMBER
$NR
$. - The current input line number for the last file handle from which you read (or performed a seek or tell on).
input_record_separator HANDLE EXPR
$INPUT_RECORD_SEPARATOR
$RS
$/ - The input record separator, newline by default.
autoflush HANDLE EXPR
$OUTPUT_AUTOFLUSH
$| - If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel. Default is 0.
output_field_separator HANDLE EXPR
$OUTPUT_FIELD_SEPARATOR
$OFS
$, - The output field separator for the print operator.
output_record_separator HANDLE EXPR
$OUTPUT_RECORD_SEPARATOR
$ORS
$\ - The output record separator for the print operator. Ordinarily the print operator simply prints out the comma-separated fields you specify, with no trailing newline or record separator assumed.
[解决办法]
|$` <$&> $ '|
| <> |是普通符号。
$`, $&, $ '是三个变量,将被匹配的字符串按匹配部分之前、匹配部分、匹配部分之后分割。
比如模式为/2/,输入123,匹配的部分是2,匹配部分前是1,匹配部分后是3,结果就是
|1 <2> 3|