http://www.perlmonks.org?node_id=939519

eddor1614 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks. I'm looking for an elegant solution to an insignificant issue, already resolved :)
I have my data like this:

abc def data 123 ghi jkl "data with spaces" 456

and I need to print something like this:

abc-def-data1 ghi-jkl-data with spaces

so, space delimits fields, but third field can have spaces if it's surrounded by double quotes.
Right now I'm doing this:

print "$1-$2-".($4 || $3) if /(\w+)\s(\w+)\s("([^"]+)"|(\w+))/;

I'm sure there should be a better way. I need $3 to have the third field, without the quotes. Anyone knows how to do it?
Thanks.