I have a file containing a long string of three digit numbers separated by commas. Like 123,123,234,345,456,654,543....

For my purposes I need to check each three digit number for a match, then extract the three digit that follows if a match is found.

My code looks like this: (in this example I am looking for the number 222 then getting the three digit number that follows after a comma)

while(<INFILE>) { while (/222,(\d\d\d),/g) { print OUTFILE "\nAFTER 222-$1"; } }

It works as expected, but after finding a match it appears that perl begins searching again AFTER the second three digit number. So if the string was 222,222,123 it would return the first result as 222 following 222, but then it starts searching again at the third number 123. I want it to also capture the second match where 123 follows the second 222. What I am hoping to do is have the seach move back 4 characters after finding a match. Hopefully this explanation makes sense?

Thanks in advance to all who provide suggestions or advice. It is much appreciated. TJ.


In reply to Shift regex search back 4 characters... by tj999

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.