锚位

Perl5 里面的正则表达式特性

\A 锚位匹配字符串的绝对开头

 

m{\Ahttps?://}i

\z 锚位匹配字符串的绝对末尾

 

m{\.png\z}i

 

\Z 他允许后面出现换行符。

while(<STDIN>){
    print if /\.png\Z/;
}

/\A\s*\Z/  #匹配一个空行

^ 行首
$ 行末


^ 和 $ 锚位可以和/m修饰符表示对多行

$_= 'This is a wilma line
barney is on another line
but this ends in fred
and a final dino line';

/fred$/m
/^barney/m

如果没有/m,^和$的行为就如同\A和\z一样。

 

单词锚位

\b是单词边界锚位,它能匹配单词的首尾

/\bfred\b/        #可匹配fred,但无法匹配frederick  alfred manfredmann
/\bhnunt/         #来匹配hunt  hunting或hunter

非单词边界锚位\B,它能匹配所有\b不能匹配的位置

/\bsearch\B/      #会匹配searches searching 与 searched,但不能匹配search
posted @ 2013-09-16 09:55  新闻官  阅读(350)  评论(0编辑  收藏  举报