On Jul 23, David Gerler said:
>How do I measure a string to see if it is greater than 85 characters?
Using length().
if (length($str) > 85) { ... }
>Basically, if the string is more than 85 characters, how do I break it at
>the last full word?
Well, it depends what you call a "word". Let's assume a word is a chunk
of non-whitespace, \S+.
if (length($str) > 85) {
$str =~ s/(.{0,85})(?<=\S)(?!\S).*/$1/s;
}
Let me break that regex down for you:
$str =~ s{
( .{0,85} ) # capture 0 to 85 characters in $1
(?<= \S ) # make sure a non-space is before us
(?! \S ) # and make sure a non-space is NOT after us
.* # match everything else
}{$1}xs; # replace with $1
# /x for comments, /s makes . match \n
You could use my Regexp::Keep module and rewrite this as
use Regexp::Keep;
$str =~ s/.{0,85}\K(?<=\S)(?!\S).*//s;
The \K escape takes the place of capturing the .{0,85} and replacing it
with itself. (Notice how the regex was s/(save)delete/$1/.)
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]