【发布时间】:2016-01-25 22:32:34
【问题描述】:
我必须处理长文本并在其中使用正则表达式进行一些替换。 现在我写了以下代码:
text = File.read(file)
replacements = [
[/```([\w]+)/, "\n\1>"],
[/```\n/, "\n\n"],
[/pattern not found/, 'sub'],
[/pattern that should be found/, 'no sub'],
]
replacements.inject(text) do |text, (k,v)|
if text =~ k
text.gsub!(k,v)
end
end
File.write(name', text)
如果在我的文档中找到每个正则表达式,一切正常,但如果没有找到替换模式,不会执行所有后续替换。
我输入了if text =~ k,但效果不一样。
有什么想法吗?
【问题讨论】: