Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://483037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-12-22 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (121 votes), past polls