String#split with regex argument

irb(main):004:0> " now’s the time".scan(/ /)
=> [" ", " “, " “, " “]
irb(main):005:0> " now’s the time”.split(/ /)
=> [””, “now’s”, “”, “the”, “time”]

I expected ["", “now’s”, “”, “the”, “”, “time”].

How
String#split(http://www.omniref.com/ruby/v2_1_1/classes/String##split)
with regex argument working here actually ?

On Tue, Mar 11, 2014 at 1:03 PM, Arup R. [email protected]
wrote:

irb(main):004:0> " now’s the time".scan(/ /)
=> [" ", " “, " “, " “]
irb(main):005:0> " now’s the time”.split(/ /)
=> [””, “now’s”, “”, “the”, “time”]

I expected ["", “now’s”, “”, “the”, “”, “time”].

How
String#split(http://www.omniref.com/ruby/v2_1_1/classes/String##split)
with regex argument working here actually ?

You have two consecutive spaces which each serve as delimiter since
you are only matching a single space. So you get an empty string in
between.

irb(main):001:0> “a b”.split(/ /)
=> [“a”, “”, “b”]

You can remedy by using a quatifier:

irb(main):002:0> “a b”.split(/ +/)
=> [“a”, “b”]

It’s probably easier to see with a different char

irb(main):003:0> “aXXb”.split(/X/)
=> [“a”, “”, “b”]
irb(main):004:0> “aXXb”.split(/X+/)
=> [“a”, “b”]

Kind regards

robert

Robert K. wrote in post #1139487:

On Tue, Mar 11, 2014 at 1:03 PM, Arup R. [email protected]

irb(main):003:0> “aXXb”.split(/X/)
=> [“a”, “”, “b”]

Okay. Let me tell you then, what I understood from it.

When first X found string has been splitted as [“a”,“Xb”]. Now on the
second unprocessed substring “Xb”, again ‘X’ matched, so it has benn
splitted as ["",“b”]. And finally ["a", "", "b"]. Am I correct ?

On Tue, Mar 11, 2014 at 1:48 PM, Arup R. [email protected]
wrote:

second unprocessed substring “Xb”, again ‘X’ matched, so it has benn
splitted as ["",“b”]. And finally ["a", "", "b"]. Am I correct ?

Roughly. I can’t say whether it is actually done that way but the
rule is simple: String#split will return everything between matches of
separator regexp.

You can imagine working like this

irb(main):001:0> def split s, re
irb(main):002:1> a = []
irb(main):003:1> pos = 0
irb(main):004:1> s.scan(re) {|m| n = $`.length; a << s[pos…n]; pos =
n + m.length}
irb(main):005:1> a << s[pos…-1]
irb(main):006:1> return a
irb(main):007:1> end
=> nil
irb(main):008:0> split “aXXb”, /X/
=> [“a”, “”, “b”]

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs