简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM