perl regexp大括号

时间:2016-06-10 07:45:28

标签: regex perl

如何匹配{和}之间的多线。我确实需要匹配下面的块

22-jul
22-jul
22-jul
22-jul
22-jul
22-jul
22-jul
22-jul
?

我必须从非常庞大的dhcpd.conf文件中删除该块。 提前谢谢!

P.S。我忘了,我需要regexp for perl ..

4 个答案:

答案 0 :(得分:0)

解决问题的方法:

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

它将替换“host customerPERA {}”上的“host customerPERA {}”(带有大括号)

答案 1 :(得分:0)

我做到了:

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

全部谢谢

答案 2 :(得分: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
 }

答案 3 :(得分:0)

您希望在(?s)本地使用single line modifier。 这可以为您提供一行代码来删除或编辑您想要的内容。

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