Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How do I return the first n words of a string?

by d_brown3 (Initiate)
on Oct 26, 2000 at 18:55 UTC ( [id://38624]=perlquestion: print w/replies, xml ) Need Help??

d_brown3 has asked for the wisdom of the Perl Monks concerning the following question:

I have this code and I need to return the first five words of the string returned in $2 by the first regular expression. The file is an xml file with a huge chunk of CDATA that contains a number of editorials split by <p> tags.
while (<INFILE>) { if (/(<editorialtext><!\[CDATA\[|<p>)(.*)(\]\]><\/editorialtext|$) +/) { my $editorial = $2; my $headline = $2; $editorial =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//gs; } }

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I return the first n words of a string?
by Fastolfe (Vicar) on Oct 26, 2000 at 18:59 UTC
    The first two things that come to my mind:
    $_ = "some string with multiple words"; @first_three = (split)[0..2]; # or ($first_three) = /^\s*(\S+\s+\S+\s+\S+)/;
    You can probably adapt either of these to your purposes.
Re: How do I return the first n words of a string?
by Anonymous Monk on Apr 16, 2002 at 13:27 UTC
    Golf swings on...
    $str = 'yo ho ho and a bottle crumbs. hangin and bangin in the upper-class slums.'; print join ' ', (split /\s+/, $str)[0..4];
Re: How do I return the first n words of a string?
by princepawn (Parson) on Oct 26, 2000 at 20:23 UTC
    Time for some Perl Golf:
    $str= 'yo ho ho and a bottle crumbs. hangin and bangin in the upper-class slums.'; @word = ($str =~ /(\w+)/g); print join ':', @word[0..4];
Re: How do I return the first n words of a string?
by runrig (Abbot) on Oct 26, 2000 at 20:18 UTC
    Here's a slightly tested solution:
    my $str="hello there bob smith and more words"; my $num_words=5; if (my ($words) = $str=~ /((?:\w+(?:\W+|$)){$num_words})/) { print $words; }
    If you want to separate the words you can of course:
    @words = split /\W+/, $words;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found