Re: string, worlds



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
.



Relevant Pages

  • Re: Prothon should not borrow Python strings!
    ... """It does not make sense to have a string without knowing what encoding ... same cul de sac as Python. ... Prothon_String_As_ASCII // raises error if there are high characters ... Python's split between byte strings and Unicode strings is ...
    (comp.lang.python)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... put them in stupid places. ... Programming is difficult (as you must surely appreciate, ... > strings will be in the range 1...1000 characters. ... impose an artificially small limit on string length." ...
    (comp.programming)
  • Re: Byte Array to String
    ... retrieved text will mismatch the original characters. ... encoding the characters. ... Dim strFileData as String ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: A note on personal corruption as a result of using C
    ... impossible to write effective string validation routines by definition ... (Note that a string literal may contain embedded null characters; ... without resorting to abusive language. ... In practice, programmers typically use "struct" ...
    (comp.programming)
  • Re: Self-Documenting Code Contest
    ... self-documenting. ... query:= 'documenting' asSortedCollection. ... string size < query size ... two words becomes a set of Characters. ...
    (comp.lang.smalltalk)