Splitting text based on whitespace

I am parsing a simple text-based data file and trying to split each line on whitespace. It should be trivial, but the fields are sometimes separated by multiple spaces or tabs. I found that whitespace can be represented as “\\s” (two backslashes), so I used something like this

val Array(name, lon, lat) = line.split("\\s")

That’s nice because it catches both spaces and tabs, but the problem is that if there are multiple spaces, each extra space produces an empty field. I realize that I can just use

val Array(name, lon, lat) = line.split("\\s").filter(_.nonEmpty)

Being the obsessive minimalist that I am, however, I want to know how to parse the multiple spaces without getting empty Strings. Anyone happen to know what the trick is? Thanks.

The string is just a regex. Use "\\s+"

1 Like