JerryMtezH
JerryMtezH

Reputation: 23

How to split a string considering more than one blank spaces in Ruby

I'm trying to get an array with the following output:

["", "7", "02156567848", "CORTIER EP. ENGERANT ROSE JOSE MARIE", "059 NOMBRE DE LA PERSONA ES DIFERENTE"]

But using next code the result is different, because split is considering any non word character to separate the strings.

a = "    7           02156567848        CORTIER EP. ENGERANT ROSE JOSE MARIE.       059 NOMBRE DE LA PERSONA ES DIFERENTE"
b = a.split(/\W\W+/)
p b

Output:

["", "7", "02156567848", "CORTIER EP", "ENGERANT ROSE JOSE MARIE", "059 NOMBRE DE LA PERSONA ES DIFERENTE"]

Any idea how to solve this?

Thanks and regards!

Upvotes: 2

Views: 46

Answers (1)

user2864740
user2864740

Reputation: 61875

Split on \s{2,} -- two or more white-space characters.

a = "    7           02156567848        CORTIER EP. ENGERANT ROSE JOSE MARIE.       059 NOMBRE DE LA PERSONA ES DIFERENTE"
a.split(/\s{2,}/)

# => ["", "7", "02156567848", "CORTIER EP. ENGERANT ROSE JOSE MARIE.", "059 NOMBRE DE LA PERSONA ES DIFERENTE"]

repl

Upvotes: 4

Related Questions