Ok I am trying to remove a very stubborn space from the beginning of my string using regex.
This string is being parsed from a CSV file into Perl using the Text:CSV module and when I print a Dumper of the string I get:
$VAR1 = ' Mgmt-General-Other';
now I have tried to use Regex to remove this space, someone will tell me to use:
$string =~ s/\s+$//;
I have already tried this as well as:
$string =~ s/\s//g;
and
$string =~ s/^\s//g;
and none of these worked, the middle one pulled every space out of everything except for the one I wanted. I’m trying to loop through a 2,000 line CSV file so I’d rather make this automated and not have to make a special case for this one weird instance.
Is there any way that this character at the beginning is not a space or a white space? Or how can I take it out?
Adding more things that I have tried;
$string =~ s/^\s+//;
here is my code:
my @value = @columns[1..12];
my $string = @value[9];
$string =~ s/^\s+//;
$string =~ s/\s+$//;
print Dumper $string;
if it matters these are my declarations at the top of the script:
use strict;
use DBI;
use Getopt::Long;
use Spreadsheet::WriteExcel;
use Spreadsheet::WriteExcel::Utility;
use Data::Dumper;
use Text::CSV;
You were pretty close actually, as the correct regex for replacing whitespace at the beginning of the string would be:
As for other solutions:
UPDATE: turned out you had a
\xA0(so-called ‘non-breakable whitespace’, which is NOT included in\s) in your string. ) Try this: