我如何在ruby中做范围的正则是awk/start/,/stop/



我想这样做一个尴尬的范围正则划分:

awk ' /hoststatus/,/}/' 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中做到这一点?

奖金:您将如何在Python中进行?

这确实很尴尬,但我是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

这是臭名昭著的触发器。

,python传递在多行和dotall标志中。这 ?遵循 *使其不盖迪

>>> import re
>>> with open('test.x') as f:
...     print re.findall('^hoststatus.*?n}$', f.read(), re.DOTALL + re.MULTILINE)