Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Splitting a string into words

by pbeckingham (Parson)
on Aug 11, 2005 at 17:00 UTC ( #483037=note: print w/replies, xml ) Need Help??

in reply to Splitting a string into words

This will extract anything up to 12 characters (greedy) that is followed by whitespace. It does what you described, but needs work to be of more general use.

#! /usr/bin/perl use strict; use warnings; my $string = 'this is a test message'; my ($chunk) = $string =~ /(.{1,12})\s/; print $chunk, "\n";

pbeckingham - typist, perishable vertebrate.

Replies are listed 'Best First'.
Re^2: Splitting a string into words
by ikegami (Pope) on Aug 11, 2005 at 17:12 UTC

    (?:\s|$) works better than \s.

    An other alternative is \b, which will chop after a word, but before punctuation.

Re^2: Splitting a string into words
by tphyahoo (Vicar) on Aug 12, 2005 at 08:31 UTC
    Depends on what a word is. If a word is \w+, this variant might be of use: ((\s|\w){0,11})(\s|$)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://483037]
[erix]: my objection to sigs is just that they take screen space. ( Some monks would have *several* slogans underneath a 1-liner reply )
[Discipulus]: a solution would be disabiltate sig as default and let users to show them
[karlgoethebier]: now it's getting weird...
[GotToBTru]: 'getting'?
[Discipulus]: why karlgoethebier ?

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2017-06-22 12:46 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (519 votes). Check out past polls.