Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby-如何将单线分隔符更改为双线分隔符?_Ruby_Regex - Fatal编程技术网

Ruby-如何将单线分隔符更改为双线分隔符?

Ruby-如何将单线分隔符更改为双线分隔符?,ruby,regex,Ruby,Regex,假设我有一个叫做“道”的文件 我想在ruby中打开它,并将其更改为: If you can talk about it, it ain't Tao. If it has a name, it's just another thing.   Tao doesn't have a name. Names are for ordinary things. 我知道如何读取文件,并将内容转换为字符串(我们称之为tao_string),但我不知道如何将单行分隔符更改为双线分隔符。通过正则表达式,我可能会

假设我有一个叫做“道”的文件

我想在ruby中打开它,并将其更改为:

If you can talk about it, it ain't Tao. If it has a name, it's just another thing.
  
Tao doesn't have a name. Names are for ordinary things.
我知道如何读取文件,并将内容转换为字符串(我们称之为
tao_string
),但我不知道如何将单行分隔符更改为双线分隔符。通过正则表达式,我可能会怀疑某些东西,但我不知道从何处开始。

您可以使用以下工具来完成此操作:

或在适当的地方:


这将用双换行符替换字符串中的所有换行符。

我发誓我以前尝试过,但无论哪种方式,效果都很好。谢谢如果您多次运行该操作会怎么样?
\n
字符数将以指数级增长@马修斯莫雷拉:是的,有什么令人惊讶的吗?这不应该被解释吗?在调用
gsub
之前,我会先打个电话。我认为这超出了这个问题的范围。
If you can talk about it, it ain't Tao. If it has a name, it's just another thing.
  
Tao doesn't have a name. Names are for ordinary things.
tao_string2 = tao_string.gsub("\n", "\n\n")
tao_string.gsub!("\n", "\n\n")