作者toolate (我是87一個)
看板Perl
標題[請益] 有條件式的取代字串
時間Thu Mar 12 09:44:05 2009
假設我的檔案中有n行 由.inc 開頭的行列
然後 我只想將這 n行中 由TT 結尾的,改成.include開頭
用了
while ($line=<file>) {
$line=~ s/.inc (TT$)/.include/g ;
push(@out,$line);
}
但對於那些TT結尾的 還是不會取代
請問我可以怎麼改
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 192.94.35.2
1F:→ toolate:在/g後面 改為/eg 會出現 compilation error 03/12 09:49
2F:→ deh3215:分兩段寫可以吧 if($_ =~ /TT$/) { 03/12 11:35
3F:→ deh3215:$_ =~ s/.inc/.include/gs; 03/12 11:36
4F:→ deh3215:檔案內容是這樣嗎 .incxxxxxxTT ? 我也算新手,互相切磋吧 03/12 11:37
5F:→ toolate:目前是用了 s/.lib (.*)(\s+TT\s*)/.include/g 03/12 11:37
6F:→ toolate:檔案是.inc 'xxxxxxxxx' TT 03/12 11:37