正則匹配首尾

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

相關文章
相關標籤/搜索