我如何在红宝石中做范围正则表达式,如awk/start/,/stop/ [英] how do i do a range regex in ruby like awk /start/,/stop/
本文介绍了我如何在红宝石中做范围正则表达式,如awk/start/,/stop/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想像这样做一个AWK风格的范围正则表达式:
I want to do an AWK-style range regex like this:
awk ' /hoststatus/,/\}/' file
在AWK中,这将在文件中打印两个模式之间的所有行:
In AWK this would print all the lines between the two patterns in a file:
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中做到这一点?
How do I do that in Ruby?
奖金:您将如何在Python中做到这一点?
Bonus: How would you do it in Python?
这在AWK中确实非常强大,但是我是Ruby的新手,所以不确定您会怎么做.在Python中,我也找不到解决方案.
This is really powerful in AWK, but I'm new to Ruby, and not sure how you'd do it. In Python I was also unable to find a solution.
推荐答案
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
这是臭名昭著的触发器.
这篇关于我如何在红宝石中做范围正则表达式,如awk/start/,/stop/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文