Rails字符串拆分

时间:2013-12-13 00:42:28

标签: ruby-on-rails

希望将数据字符串(参数)拆分为数组,但要对其进行一些特定的标准。我的字符串如下所示:

cyr_ad_id\tproject_number\tname\tremote_reference\tsample_size\tad_length\tsample_description\tmedia_type\tnotes\r\n13342a\t13342\tMore Dad_BC\t2897855894001\t150\t:30\t50% Customers\r\n50% Non-customers\tFilm\tBroadcast\r\n13342c\t13342\tDRTV - Hogs\t2897815438001\t150\t:60\t100% Non-customers\tFilm\tBroadcast\r\n13342d\t13342\tMake Way For More\t2897815439001\t150\t:30\t50% Customers\r\n50% Non-customers\tFilm\tBroadcast\r\n

我希望得到以下结果:

["cyr_ad_id\tproject_number\tname\tremote_reference\tsample_size\tad_length\tsample_description\tmedia_type\tnotes", "13342a\t13342\tMore Dad_BC\t2897855894001\t150\t:30\t50% Customers\r\n50% Non-customers\tFilm\tBroadcast", "13342c\t13342\tDRTV - Hogs\t2897815438001\t150\t:60\t100% Non-customers\tFilm\tBroadcast", "13342d\t13342\tMake Way For More\t2897815439001\t150\t:30\t50% Customers\r\n50% Non-customers\tFilm\tBroadcast"]

我觉得我需要做的是类似于在第8次出现“\ t”后将字符串拆分为“\ r \ n”。第8次出现是我想通过变量传递给split语句的东西。

我可能不希望将此字符串拆分为“\ r \ n”,因此在此示例中,为什么第n次或第8次出现“\ t”是至关重要的。

谢谢!

2 个答案:

答案 0 :(得分:0)

这是否符合您的需要?

def ssplit str, n
  r = Regexp.new "([^\t]*?\t){#{n}}(.*?\r\n)"
  matched = []

  while s = str.slice!(r) do
    matched << s.strip
  end

  matched
end

答案 1 :(得分:0)

your_string.gsub("\r\n", "\t").gsub("\n", '').split("\t").each_slice(8).to_a

将所有\r\n替换为\t,然后删除任何遗留在\n以上的内容。拆分\t。现在你有了一系列的物品。

你也可以使用正则表达式,我似乎无法开始工作。 .split(/\\r|\\t|\\n/)

.each_slice(8).to_a一次需要8个项目并重新构建它们,因此每个组都有一个数组。

您也可以.in_groups_of(8)代替.each_slice。也不需要.to_a

[["cyr_ad_id", "project_number", "name", "remote_reference", "sample_size", "ad_length", "sample_description", "media_type"], ["notes", "13342a", "13342", "More Dad_BC", "2897855894001", "150", ":30", "50% Customers"], ["50% Non-customers", "Film", "Broadcast", "13342c", "13342", "DRTV - Hogs", "2897815438001", "150"], [":60", "100% Non-customers", "Film", "Broadcast", "13342d", "13342", "Make Way For More", "2897815439001"], ["150", ":30", "50% Customers", "50% Non-customers", "Film", "Broadcast"]]

这看起来更有用吗?