How can I replace multiple whitespace with a single space in Perl?

前端 未结 3 1596
心在旅途
心在旅途 2021-02-19 06:17

Why is this not working?

$data = \"What    is the STATUS of your mind right now?\";

$data =~ tr/ +/ /;

print $data;
3条回答
  •  野的像风
    2021-02-19 06:51

    Perl 5.10 has a new character class, \h, the stands for horizontal whitespace which is nice for this sort of thing:

     $s =~ s/\h+/ /g;
    

提交回复
热议问题