Ruby 为什么';t---.*--选择介于---和---之间的所有内容?

Ruby 为什么';t---.*--选择介于---和---之间的所有内容?,ruby,regex,Ruby,Regex,我有一个如下所示的文本文件: Some text here. This text is not replaced. --- And then a wild block appears! It has stuff in it that I'm trying to replace. --- The block is no more. Nothing to replace here. 我想替换--和--之间的所有内容。所以我试着: text=File.open('myfile') text

我有一个如下所示的文本文件:

Some text here. This text is not replaced.

---

And then a wild block appears!
It has stuff in it that I'm trying to replace.

---

The block is no more. Nothing to replace here. 
我想替换
--
--
之间的所有内容。所以我试着:

text=File.open('myfile')
text.sub(/---.*---/, 'replacement')
但这似乎不起作用。我做错了什么

您需要在图案中指定“多行”选项,以使点符号与换行符匹配:

因此,您的代码应该如下所示

text.sub(/---.*?---/m, 'replacement')
请参见

您需要在图案中指定“多行”选项,以使点符号与换行符匹配:

因此,您的代码应该如下所示

text.sub(/---.*?---/m, 'replacement')

你是说
/m
(多行)吗?另外,如果有多个
“--”
分隔的块,可以将
*
设为非贪婪(
*?
)。我不相信Ruby支持
s
选项。@CarySwoveland是的,没错。这很奇怪,但“点符号匹配换行符”是Ruby中的多行模式,而不是其他语言中的单行模式。我的错,更新答案。你是说
/m
(多行)吗?另外,如果有多个
“--”
分隔的块,可以将
*
设为非贪婪(
*?
)。我不相信Ruby支持
s
选项。@CarySwoveland是的,没错。这很奇怪,但“点符号匹配换行符”是Ruby中的多行模式,而不是其他语言中的单行模式。我的错,更新答案。