in reply to Replace a word in a line except for the last char of the word?

Hi, just try the below code (is this meet your requirement?)

my $line = '<br> 2<br> 3<br> 4'; my $temp = $line; while ($temp =~m#(<br>)(.+?)( \d+)#gsi) { my $full= $&; my $text=$1; my $space = "$2"; my $digit = "$3"; my @arr=(); (@arr) = $space =~s#( )# #gsi; my $nbsptext = "&nbsp;" x "@arr"; $line =~s#$full#${text}${nbsptext}${digit}#gsi; $full=""; } print $line; output: <br>&nbsp; 2<br>&nbsp;&nbsp; 3<br>&nbsp;&nbsp; 4
  • Comment on Re: Replace a word in a line except for the last char of the word?
  • Download Code