正则匹配首尾

1 匹配行首   '^'   or  '\A'断言this

区别:当使用m(多行)修饰符时,^匹配每行的行首,而\A仍然仅在整个字符串的开头进行匹配字符串

eg:file

$_ = "this is\na multi file";字符

s/^/BOL/mg;   

# BOL this is

# BOL a multi file

s/\A/BOL/mg;

# BOL this is

# a multi file

2 匹配行尾 '$'   or  '\z'/'\Z'

'$' 在行尾以前进行匹配

eg:

$_ = 'Here is some text\n';

/(.*$)/;   

print "${1}."

##"Here is some text.";

 

/(.*$)/s;    ##用/s处理新行 

print "${1}."

##  "Here is some text

##   .";

 

'$'和'\z''\Z'的区别同'^' '\A'

 

3 '\Z' '\z' 的区别

'\z' 仅仅匹配字符串的末尾

'\Z'仅仅匹配字符串末尾或字符串末尾新行以前

$_ = "here is some text\n";

s/\z/END/;   ##匹配'\n'以前的内容

##here is some text

##END

s/\Z/END/;   ##匹配字符串结尾

##here is some text END

相关文章
相关标签/搜索