tj999 has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Shift regex search back 4 characters...
by Athanasius (Archbishop) on Mar 04, 2017 at 06:54 UTC | |
|
Re: Shift regex search back 4 characters...
by tybalt89 (Monsignor) on Mar 04, 2017 at 04:07 UTC | |
by tj999 (Novice) on Mar 04, 2017 at 09:45 UTC | |
|
Re: Shift regex search back 4 characters...
by kcott (Archbishop) on Mar 04, 2017 at 09:41 UTC | |
by BillKSmith (Monsignor) on Mar 04, 2017 at 15:37 UTC | |
by AnomalousMonk (Archbishop) on Mar 04, 2017 at 20:53 UTC | |
|
Re: Shift regex search back 4 characters...
by tybalt89 (Monsignor) on Mar 04, 2017 at 22:39 UTC | |
by AnomalousMonk (Archbishop) on Mar 13, 2017 at 05:47 UTC | |
|
Re: Shift regex search back 4 characters...
by ablanke (Monsignor) on Mar 08, 2017 at 10:54 UTC |