in reply to Truncating real text
use strict; my $wr = qr/[-\w.,;:'"()]+/; # needs tuning sub trunc { my $max = shift or return ''; my $text = ' ' . shift or return ''; $text =~ s/^((?:\s+$wr){0,$max}).*$/$1/; substr($text, 0, 1) = ''; $text =~ s/\([^)]*$//; return $text; } DB<1> p trunc(5, " 'Twas (brillig), and the (slithy toves) did gyre a +nd gimble in the wabe:" 'Twas (brillig), and the
The problem is too ill-defined (how is "word"defined? how is "punctuation" defined?, can the input contain leading spaces?, etc.) to warrant golfing it.
The hack of prepending the space and then lopping it off simplifies the regexp.
The word regexp $wr probably can use some tweaking.
the lowliest monk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Truncating real text
by gam3 (Curate) on Mar 16, 2005 at 18:45 UTC |