有关perl正则表达式的一些杂项


$也能匹配n

见Perl语言入门,page 132, 注释6

1 /^.*$/能匹配"n"么?能!因为$不仅能匹配行尾,也能匹配n
2 /^.*$/能匹配"bn"么?能!.能b匹配. n匹配$
3 /^.*$/能匹配"nb"么?不能!为什么?因为默认情况下,.不能匹配n,把模式改一下变成/^.*$/s就可以了,/s表示.能匹配任意字符,包括n

多行匹配/m

看一个例子,这段代码输出:hello


my $text = "hello, world,nhello zdd,nhello autumn";
while ( $text =~ /^hello/g ) {
print "hellon"
}

稍微改变一下,加上/m选项


my $text = "hello, world,nhello zdd,nhello autumn";
while ( $text =~ /^hello/mg ) {
print "hellon"
}

现在输出变成了
hello
hello
hello

注解:
默认情况下,^和$匹配整个字符串的开头和结尾,但是加上/m之后,^和$就匹配每行的开头和结尾。也就是说,因为字符串中有换行符n,所以/m选项使得^$匹配每行的开头和结尾。
如果字符串中没有换行符,那么/m选项是不起作用的。

perl中heredoc使用说明
格式printEOFyoutextgohereEOFsubusage{printEOF;Usage:test.pl-cconfig,-ffile-llines-cconfigfile-ffilename-lnumberoflinesEOF}NOTE:thelastEOFmuststartatthebeginningoftheline!!!youcanuseotherwordsinstead

Perl使用chdir的实例代码
usestrict;usewarnings;#Printallfilesinadirectorysubprint_files{my$dir='d:/code';opendirDIR,$dirordie$!;my@files=readdirDIR;chdir$dir;#Usechdiror-fwillnotwork,since-fneedabsolutelypathforeachmy$file(@f

perl特殊符号及默认的内部变量
Perl的特殊符号@数组$x{}x名字前面是美元符号($),后面是花括号({}),则其为hash元素%要引用整个hash,使用百分号()作为前缀。前面几页中使用的hash的名字为%fa