Ruby regex matching a line in an inputted text file string [duplicate]

拜拜、爱过 提交于 2019-12-06 09:26:28
string = "test\r\nfoo\r\ntest\r\nbar"
string = string.gsub(/^test(?=\r?\n)/, 'X\&X').delete(?\r)
puts string

It works fine for me in Ruboto IRB on my phone. Could you make a self-contained one-line example and post it? No file IO should be necessary. What ruby version do you use?

I suspect your program just has a typo.

Problem is because of dos line endings(\r\n), so for getting rid of this, there is a flip command (in MAC, if I'm correct) for converting dos line endings to mac line endings(\r). Search for it and convert dos line endings to mac line endings before using that text file.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!