in reply to regexp problem

maybe you can trim the spaces at the ends first ?
s/^\s+//, s/\s+$// for $string;