You're right. I completely misread the question. split seems a likely candidate:

#! perl -slw use strict; while( <DATA> ) { my @words = split /[^a-zA-Z']+/; for ( 0 .. $#words ) { printf "%s, $., %s %s %s\n", $words[ $_ ], $_ ? $words[ $_ -1 ] : '', $words[ $_ ], $_ < $#words ? $words[ $_ + 1 ] : '' ; } } __DATA__ little boy blue come blow on your horn give a little, take a lot give a lot, take a little Mary had a little lamb Don't belittle the little guy

Outputs

[14:37:07.78]C:\test>junk4 little, 1, little boy boy, 1, little boy blue blue, 1, boy blue come come, 1, blue come blow blow, 1, come blow on on, 1, blow on your your, 1, on your horn horn, 1, your horn give, 2, give a a, 2, give a little little, 2, a little take take, 2, little take a a, 2, take a lot lot, 2, a lot give, 3, give a a, 3, give a lot lot, 3, a lot take take, 3, lot take a a, 3, take a little little, 3, a little Mary, 4, Mary had had, 4, Mary had a a, 4, had a little little, 4, a little lamb lamb, 4, little lamb Don't, 5, Don't belittle belittle, 5, Don't belittle the the, 5, belittle the little little, 5, the little guy guy, 5, little guy

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^3: Finding word either side of a word match by BrowserUk
in thread Finding word either side of a word match by Quicksilver

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.