PracticeDev/study_perl/re/16re.pl

12 lines
493 B
Perl
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/perl
# 通过3个特殊变量$`、$&和$'可以保存匹配内容之前的内容,
# 匹配内容以及匹配内容之后的内容。
# 但是只要使用了这3个变量中的任何一个后面的所有分组效率都会降低。
# perl提供了一个p修饰符能实现完全相同的功能
$ans="cat sheep tiger";
$ans =~ /sheep/p;
print "${^PREMATCH}\n"; # 输出"cat "
print "${^MATCH}\n"; # 输出"sheep"
print "${^POSTMATCH}\n"; # 输出" tiger"