我如何通过\ n吐出线条

时间:2017-07-04 09:43:19

标签: ruby regex

我想将这一行拆分为\n

a = "Id, Name, Age, Address\n123,John,23,\"No:23 21st street\n areacode\npincode\"\n124,Antony,25,\"No:213 29th street\n areacode\npincode\"\n"

用\ n分割线后,该线应该是这样的。

a = ["Id, Name, Age, Address", "123,John,23,\"No:23 21st street\n areacode\npincode\"", "124,Antony,25,\"No:213 29th street\n areacode\npincode\"]

我试过的命令

a.scan(/(?!\B"[^"]*)^C?(?:[\n\r]+|\z)(?![^"]*"\B)/)
a.scan(/.+?(?:[\n\r]+|\z)/)

1 个答案:

答案 0 :(得分:1)

为什么不使用split中的String方法?

a = "Id, Name, Age, Address\n123,John,23,\"No:23 21st street\n areacode\npincode\"\n124,Antony,25,\"No:213 29th street\n areacode\npincode\"\n"

a.split "\n"

["Id, Name, Age, Address", "123,John,23,\"No:23 21st street", " areacode", "pincode\"", "124,Antony,25,\"No:213 29th street", " areacode", "pincode\""]