Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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.

Comment on Re: Splitting a string into words
Download Code
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]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2015-11-27 23:45 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (734 votes), past polls