Re: string, worlds
- From: japhy@xxxxxxxxxxxx (Jeff 'japhy' Pinyan)
- Date: Wed, 25 May 2005 09:48:16 -0400 (EDT)
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 .
- References:
- string, worlds
- From: Ing. Branislav Gerzo
- string, worlds
- Prev by Date: RE: Can't use subscript in angle brackets
- Next by Date: Re: while loop -> map
- Previous by thread: string, worlds
- Next by thread: output in a single line
- Index(es):
Relevant Pages
|