sub stripPOS { my $words = shift; # rip out any / plus following characters, up to the # first space $words =~ s!/\S*!!g; return $words; } $sentence =~ s! \s \ (.*?) \ \s ! stripPOS($1). '/NP' !egx;