On May 25, Ing. Branislav Gerzo said:
hello, very common problem is delimite some strings at certain words,
lets say:
$string = "this is some small test";
Now, we define, the longest $string2 could be 10 characters, and
$string we could delimite only on spaces and optionally punctation
(not on characters).
So you have a string that you want to make no longer than a certain number
of characters, but NOT cut it off in the middle of a word? That depends
how you define "word". If the string was "this mini-game is fun" and you
cut it off at 10 characters, would "this mini-" be ok?
my ($string2) = $string =~ /^(.{0,10})(?:[\W]|$)/;
You don't need to [...] the \W.
/^(.{0,10})(?:\W|$)/
I would probably just write that as:
/^(.{0,10})\b/
If the last of the 10 characters is a letter and the character AFTER it is
a letter, the \b (word boundary) anchor will fail, and the regex will
backtrack until it reaches a point where there's a word character on one
side and a non-word character on the other.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>