Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can perl replace multiple substrings with regex?

I have some substring [ aa bb cc ] in a line, like $line = "1 2 a b [ aa bb cc ] c d [ bb cc ] 3 4". And I want to trim all the spaces in these substrings. The following code does not work.

while($line =~ /\[(.*?)\]g/)
{
  $1 =~ s/\s+//g;
}

Can someone help please

like image 382
user2309694 Avatar asked May 04 '26 06:05

user2309694


1 Answers

s{\[(.*?)\]}{
   my $s = $1;
   $s =~ s/\s+//g;
   $s
}eg;
like image 166
ikegami Avatar answered May 06 '26 01:05

ikegami