My first approach wouldn't be to do this with a single regexp. Instead, consider the following:
- If the string length is <= 45 characters, it wins
- Otherwise, extract the first 46 characters, and apply
$str =~ s/\s\S*?$//;
which trims the string back to a word boundary. This doesn't guarantee the that resulting string will be 45 characters or less, though, since someone could provide a long string with no whitespace.
You might also consider things like first deleting leading spaces, and collapsing multiple spaces into one.