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 }; #### c:\@Work\Perl\monks>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; note 'perl version: ', $]; ;; use constant S => ' Intel(R) Xeon(R) CPU X5660 2.80GHz '; use constant T => 'Intel(R) Xeon(R) CPU X5660 2.80GHz'; ;; for my $rxSpaces ( qr{ (?<= \s) \s+ | \A \s+ | \s+ \z }xms, qr{ \A \s+ | (?<= \s) \s+ | \s+ \z }xms, qr{ \A \s+ | \s+ \z | (?<= \s) \s+ }xms, ) { (my $t = S) =~ s{$rxSpaces}{}g; ok $t eq T, qq{$rxSpaces -> \n >$t<}; } ;; note qq{still with spaces? >${ \S }<}; done_testing; " # perl version: 5.008009 ok 1 - (?msx-i: (?<= \s) \s+ | \A \s+ | \s+ \z ) -> # >Intel(R) Xeon(R) CPU X5660 2.80GHz< ok 2 - (?msx-i: \A \s+ | (?<= \s) \s+ | \s+ \z ) -> # >Intel(R) Xeon(R) CPU X5660 2.80GHz< ok 3 - (?msx-i: \A \s+ | \s+ \z | (?<= \s) \s+ ) -> # >Intel(R) Xeon(R) CPU X5660 2.80GHz< # still with spaces? > Intel(R) Xeon(R) CPU X5660 2.80GHz < 1..3 ok 4 - no warnings 1..4 #### c:\@Work\Perl\monks>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; note 'perl version: ', $]; ;; use constant S => ' Intel(R) Xeon(R) CPU X5660 2.80GHz '; use constant T => 'Intel(R) Xeon(R) CPU X5660 2.80GHz'; ;; for my $rxSpaces ( qr{ (?<= \s) \s+ | \A \s+ | \s+ \z }xms, qr{ \A \s+ | (?<= \s) \s+ | \s+ \z }xms, qr{ \A \s+ | \s+ \z | (?<= \s) \s+ }xms, qr{ \s \K \s+ | \A \s+ | \s+ \z }xms, qr{ \A \s+ | \s \K \s+ | \s+ \z }xms, qr{ \A \s+ | \s+ \z | \s \K \s+ }xms, ) { (my $t = S) =~ s{$rxSpaces}{}g; ok $t eq T, qq{$rxSpaces -> \n >$t<}; } ;; note qq{still with spaces? >${ \S }<}; done_testing; " # perl version: 5.010001 ok 1 - (?msx-i: (?<= \s) \s+ | \A \s+ | \s+ \z ) -> # >Intel(R) Xeon(R) CPU X5660 2.80GHz< ok 2 - (?msx-i: \A \s+ | (?<= \s) \s+ | \s+ \z ) -> # >Intel(R) Xeon(R) CPU X5660 2.80GHz< ok 3 - (?msx-i: \A \s+ | \s+ \z | (?<= \s) \s+ ) -> # >Intel(R) Xeon(R) CPU X5660 2.80GHz< not ok 4 - (?msx-i: \s \K \s+ | \A \s+ | \s+ \z ) -> # > Intel(R) Xeon(R) CPU X5660 2.80GHz < # Failed test '(?msx-i: \s \K \s+ | \A \s+ | \s+ \z ) -> # > Intel(R) Xeon(R) CPU X5660 2.80GHz <' # at -e line 1. not ok 5 - (?msx-i: \A \s+ | \s \K \s+ | \s+ \z ) -> # >Intel(R) Xeon(R) CPU X5660 2.80GHz < # Failed test '(?msx-i: \A \s+ | \s \K \s+ | \s+ \z ) -> # >Intel(R) Xeon(R) CPU X5660 2.80GHz <' # at -e line 1. ok 6 - (?msx-i: \A \s+ | \s+ \z | \s \K \s+ ) -> # >Intel(R) Xeon(R) CPU X5660 2.80GHz< # still with spaces? > Intel(R) Xeon(R) CPU X5660 2.80GHz < 1..6 ok 7 - no warnings 1..7 # Looks like you failed 2 tests of 7.