2016-06-10 227 views
0

如何匹配{和}之間的多行。確實我需要匹配下面的塊perl正則表達式大括號

host customerPERA { 
     host-identifier option agent.circuit-id "x:y"; 
     fixed-address yyy.yyy.yyy.yyy; 
     option routers xxx.xxx.xxx.xxx; 
     option subnet-mask 255.255.255.0; 
} 

我必須從非常大的dhcpd.conf文件中刪除該塊。 在此先感謝!

P.S.我忘了,我需要的正則表達式爲Perl ..

回答

0

我做到了:

perl -pe 'BEGIN{undef $/;} s/^host customerPERA[^}]*}//smg' /etc/dhcp/dhcpd.conf 

感謝所有

+0

如果在輸入內發現任何大括號,那麼怎麼辦?可能會不符合? – ssr1012

+0

@ ssr1012:我配置dhcpd服務器已經很長時間了,但是如果我正確記得,沒有任何在主機定義中使用大括號的實例。但我可能是錯的 – stevieb

0

的解決你的問題:

perl -p0i -e 's/host\s+customerPERA\s+\{[^}]+\}/host customerPERA {}/g' /etc/dhcp/dhcpd.conf 

它將取代 「主機customerPERA {}」 上的 「主機customerPERA {}」(空大括號)

+2

可能還需要/ s多行也 – xxfelixxx

+2

@xxfelixxx:/ s改變'.'行爲的方式。正則表達式中沒有'.'。 – choroba

+2

'/ m'改變'^'和'$'的行爲方式。這裏不需要。 – choroba

0

我能做到這樣:

輸入:

host customerPERA{ 
     host-identifier option agent.circuit-id "x:y"; 
     fixed-address yyy.yyy.yyy.yyy; 
     option routers xxx.xxx.xxx.xxx; 
     option subnet-mask 255.255.255.0; 
} 

如果這是找到的情況下精確匹配:

正則表達式:

$myRegex = qw/((?:[^{}]*(?:{(?:[^{}]*(?:{(?:[^{}]*(?:{[^{}]*})*[^{}]*)*})*[^{}]*)*})*[^{}]*)*)/; 

if($pattern=~m/host CustomerPERA\{$myRegex\}/gs) 
{ 
     #do it you statement here 
} 
0

你想與(?s)本地使用single line modifier。 這可以給你一個行代碼來刪除或編輯你想要的。

$string =~ s/host customerPERA \{(?s).+?\}//;