in reply to Smart Substrings

Only one match needed.

if (length($string) > 15) { $string =~ s/^(.{0,12})\b\s.*/$1.../gi; }

cLive ;-)

Update:

MeowChow pointed out typo - missed out .* (had it on other machine running demo :)

Actually, if there's a chance that the first word will be longer than 15 chars, you'd need to expand this a bit to cut off the end of the first word. Oh hell, let's change a few things here:

if (length($string) > 15) { unless ($string =~ s/^(.{0,12})\b\s.*/$1.../gi) { $string = substr ($string, 0, 12) . '...'; } }

Finally, I disagree with MeowChow (below). I think \b *is* needed. Otherwise this could happen:

A Satsuma - Orange would become A Satsuma -... </CODE>

Replies are listed 'Best First'.
Re: Re: Smart Substrings
by MeowChow (Vicar) on Apr 03, 2001 at 05:49 UTC
    This is broken. You left out the .* at the end of your substitution to eat up the rest of the string, and if you put it in, you will find that it still breaks on input such as "antidisestablishment". Also, your \b is superfluous.

    Remember, just say NO to regex (when simpler functions will do).

       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print