-4

I have a requirement to remove the leading and trailing spaces in each column of a string delimited by "|"

TEST| 100| 0.00 |TEST STRING I am using the following regular expressions to remove the spaces, but it does not seem to work.

$data = "TEST|               100|            0.00 |TEST STRING    ";
$data =~ s/(^|\|)\s+/\1/g;
$data =~ s/\s+$//;

Any help would be appreciated.

senthilkumar
  • 13
  • 2
  • 5

1 Answers1

0

This should work to remove all trailing and leading spaces for each column data, including the first one.

$data = " TEST  |               100|            0.00 |TEST STRING    ";
$data =~ s/\s*\|\s*/|/g;
$data =~ s/\s+$//;
$data =~ s/^\s+//;
print "$data\n";
Lye Heng Foo
  • 1,779
  • 1
  • 10
  • 8