微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 如何在ruby中执行范围正则表达式,如awk / start /,/ stop /

我想像这样做一个AWK风格的范围正则表达式:

awk ' /hoststatus/,/\}/' file

在AWK中,这将打印文件中两个模式之间的所有行:

hoststatus {
host_name=myhost
modified_attributes=0
check_command=check-host-alive
check_period=24x7
notification_period=workhours
check_interval=5.000000
retry_interval=1.000000
event_handler=
}

我如何在Ruby中做到这一点?

额外奖励:你会怎么用Python做的?

这在AWK中非常强大,但我是Ruby的新手,并不确定你是如何做到的.在Python中,我也找不到解决方案.

解决方法:

ruby:

str =
"drdxrdx
hoststatus {
host_name=myhost
modified_attributes=0
check_command=check-host-alive
check_period=24x7
notification_period=workhours
check_interval=5.000000
retry_interval=1.000000
event_handler=
}"
str.each_line do |line|
  print line if line =~ /hoststatus/..line =~ /\}/
end

这是臭名昭着的flip-flop.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐