python - 我如何在 awk/start/,/stop/这样的 ruby​​ 中做一个范围正则表达式

标签 python ruby regex awk

我想像这样做一个 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 .

关于python - 我如何在 awk/start/,/stop/这样的 ruby​​ 中做一个范围正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12980500/

相关文章:

python - 如何从 django-tenant-schemas 转储数据?

javascript - Rails 5 - turbolinks 5,一些 JS 未在页面呈现时加载

ruby - 使用/.*有什么好处?/

ruby - 添加 gem "tesseract-ocr"Rails 4 后尝试运行服务器时出错

javascript - 简单的正则表达式Excel提取

regex - 获取一个在正则表达式匹配之前的单词?

python - 在 numpy 中向量化求根

python - Z3 字符串/字符异或?

python - 当从遗留数据库生成模型类时,如何在 django-rest 中序列化多个模型数据

用于表单验证的正则表达式 1-9999