Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2021-04-10 21:38 GMT
Find Nodes?
    Voting Booth?

    No recent polls found