Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Splitting a string into words

by pbeckingham (Parson)
on Aug 11, 2005 at 17:00 UTC ( [id://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 (Patriarch) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://483037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-23 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found