小编ran*_*183的帖子

在Ruby中使用split函数时的答案差异

鉴于以下输入:

line1 = "Hey | Hello | Good | Morning"
line2 = "Hey , Hello , Good , Morning"
file1=length1=name1=title1=nil
Run Code Online (Sandbox Code Playgroud)

使用','将字符串拆分如下:

file1, length1, name1, title1 = line2.split(/,\s*/)
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

puts file1,length1,name1,title1

>Hey
>Hello
>Good
>Morning
Run Code Online (Sandbox Code Playgroud)

但是,使用'|' 分割字符串我收到不同的输出:

file1, length1, name1, title1 = line2.split(/|\s*/)
puts file1,length1,name1,title1

>H
>e
>y
Run Code Online (Sandbox Code Playgroud)

除了分隔符号(第一种情况下为逗号,第二种情况下为管道)外,两个字符串都相同.我正在使用的分割函数的格式也是相同的,当然,除了分隔字符.是什么导致这种变化?

ruby regex split

4
推荐指数
1
解决办法
109
查看次数

标签 统计

regex ×1

ruby ×1

split ×1