Removing repeated characters, including spaces, in one line

Posted by Thumper on Stack Overflow See other posts from Stack Overflow or by Thumper
Published on 2012-10-13T21:13:53Z Indexed on 2012/10/13 21:37 UTC
Read the original article Hit count: 146

Filed under:
|


I currently have a string, say $line='55.25040882, 3,,,,,,', that I want to remove all whitespace and repeated commas and periods from. Currently, I have:

    $line =~ s/[.,]{2,}//;
    $line =~ s/\s{1,}//;

Which works, as I get '55.25040882,3', but when I try

$line =~ s/[.,\s]{2,}//;

It pulls out the ", " and leaves the ",,,,,,". I want to retain the first comma and just get rid of the whitespace.
Is there a way to elegantly do this with one line of regex? Please let me know if I need to provide additional information.

© Stack Overflow or respective owner

Related posts about regex

Related posts about perl