use strict; use warnings; use feature qw{ say }; my $str = q{ Intel(R) Xeon(R) CPU X5660 2.80GHz }; my $rxSpaces = qr{(?x) # Use regex extended syntax to allow comments (?: # Open non-capturing group for alternation (?<= \A ) \s+ # Spaces preceded by beginning of string | # or (?<= \s ) \s+ # Spaces preceded by a single space | # or \s+ (?= \z ) # Spaces followed by end of string ) # Close group }; # Replace matching spaces by nothing globally. # $str =~ s{$rxSpaces}{}g; say qq{-->$str<--}; #### -->Intel(R) Xeon(R) CPU X5660 2.80GHz<--