in reply to (tye)Re: Slicing a string on words
in thread Slicing a string on words

Very nice solution, tye!!

There's a small typo in the regex (a surplus parenthesis), so here goes the code again, corrected

$window_size--; my( $windowed_text )= $text_body =~ /^\s*(?:\S+\s+){$window_start}(\S+(?:\s+\S+){0,$window_size})/;
What I wanted to add is that Perl handles the boundary cases very nicely, so no extra handling required for $window_start = 0 or $window_size = 0. This means
$_ = q/01234/; /^..{0}/; # matches '0' /^..{0,0}/; # matches '0' /^..{0,-1}/; # doesn't match at all
which is exactly what we need for the code to work fine.

-- Hofmator

Replies are listed 'Best First'.
(tye)Re2: Slicing a string on words
by tye (Sage) on Aug 28, 2001 at 21:31 UTC

    Thanks. Note that $window_size of 0 will probably behave the same as $window_size of 1, though.

    Update: Sorry, wrong. I misunderstood your examples. /..{0,-1}/ matches any two characters followed by the literal string "{0,-1}". This could still be a problem for certain input values, but such seem pretty unlikely. So you might want some special code for the boundary case, depending on how varied your inputs might be.

            - tye (but my friends call me "Tye")