Win8 Strawberry 5.8.9.5 (32) Thu 12/17/2020 21:09:41 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -MData::Dump=dd my $s = " info John 100 - 2000 Kent"; my $word = ''; while ($s =~ m{ $word [^[:alnum:]]+ ([[:alnum:]]+) }xms) { print "next word after '$word' is '$1' \n"; $word = $1; } print "another way \n"; my @words = $s =~ m{ [[:alnum:]]+ }xmsg; dd \@words; ^Z next word after '' is 'info' next word after 'info' is 'John' next word after 'John' is '100' next word after '100' is '2000' next word after '2000' is 'Kent' another way ["info", "John", 100, 2000, "Kent"]

Update 1: The first method above will fail to capture 'info' if there are no "non-word" (whitespace in this case) characters before the first word in the string. To capture the first word in this case, use
    $s =~ m{ $word [^[:alnum:]]* ([[:alnum:]]+) }xms
(note * quantifier on [^[:alnum:]]* vice +).
However, using $word as an anchor then fails if there is a repeated "word" in the string: try replacing "100" with another instance of "info" and see what happens. IMHO, "another way" is the better way to strip out "words" from a string.

Update 2: Here's a way to loop through the string word-by-word regardless of leading/trailing whitespace or repeated words (but I still prefer stripping/extracting all words to an explicit or implicit array - the second method above):

Win8 Strawberry 5.8.9.5 (32) Thu 12/17/2020 21:59:18 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $s = " info John info - 2000 Kent "; my $word; while ($s =~ m{ [^[:alnum:]]* ([[:alnum:]]+) }xmsg) { if (defined $word) { print "next word after '$word' is '$1' \n"; } else { print "first word is '$1' \n"; } $word = $1; } ^Z first word is 'info' next word after 'info' is 'John' next word after 'John' is 'info' next word after 'info' is '2000' next word after '2000' is 'Kent'


Give a man a fish:  <%-{-{-{-<


In reply to Re: getting next word or number after another (updated x2) by AnomalousMonk
in thread getting next word or number after another by bigup401

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.