Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

( #38624=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie d_brown3 on Oct 26, 2000 at 18:55 UTC
Q&A  > strings


Description:

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; } }

Answer: How do I return the first n words of a string?
contributed by Fastolfe

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.
Answer: How do I return the first n words of a string?
contributed by Zombie Wyzelli

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];
Answer: How do I return the first n words of a string?
contributed by princepawn

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];
Answer: How do I return the first n words of a string?
contributed by runrig

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;

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (10)
    As of 2014-10-24 09:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (131 votes), past polls