使用Ruby,我如何在两种情况下分开 - 有3个或更多空格还是标签字符?我试过这个
2.4.0 :003 > line = "a\tb\tc"
=> "a\tb\tc"
2.4.0 :004 > line.split(/([[:space:]][[:space:]][[:space:]]+|\t)/)
=> ["a", "\t", "b", "\t", "c"]
但正如您所看到的,标签字符本身已包含在我的结果中。结果应该是
["a", "b", "c"]
答案 0 :(得分:2)
仅split
怎么样?
p "a\tb\tc".split
# ["a", "b", "c"]
p "a\tb\tc\t\tc\t\t\t\t\t\t\tc\ts\ts\tt".split
# ["a", "b", "c", "c", "c", "s", "s", "t"]
虽然当有三个或更多三个空格时,它不会分裂,但这可能会有效:
p "a\tb\tc\t\tc\t\t\ t\t\tc\ts\ts\tt".split(/\s{3,}|\t/)
# => ["a", "b", "c", "c", "t", "c", "s", "s", "t"]
答案 1 :(得分:1)
line = "aa bb cc\tdd"
line.split /\p{Space}{3,}|\t+/
#⇒ ["aa bb", "cc", "dd"]