in reply to regexp word break help
if (length $word > 45) { my $forty_sixth = substr($word, 45, 1); $word = substr($word, 0, 45); $word =~ s/\s*\w*\z// if $forty_sixth =~ /\w/; }
Update: For some reason I was thinking about this while trying to get to sleep last night. My solution will fail to trim whitespace in the case where the 45th and 46th chars are both whitespace. Also, I should know better than to write a substitution expression that can match the empty string. Here's a revised version that should work correctly:
if (length $word > 45) { my $forty_sixth = substr($word, 45, 1); $word = substr($word, 0, 45); $word =~ s/\w+\z// if $forty_sixth =~ /^\w/; $word =~ s/\s+\z//; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: regexp word break help
by dsb (Chaplain) on Apr 30, 2002 at 21:33 UTC | |
by thelenm (Vicar) on Apr 30, 2002 at 21:51 UTC |