in reply to Shift regex search back 4 characters...

In the spirit of TIMTOWTDI, here is one way to actually move the search point back.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1183627 use strict; use warnings; $_ = '222,345,222,678,222,222,543,111'; while( /222,(\d\d\d)/g ) { print "AFTER 222-$1\n"; pos($_) -= 4; # actually move back 4 characters }

Replies are listed 'Best First'.
Re^2: Shift regex search back 4 characters...
by AnomalousMonk (Archbishop) on Mar 13, 2017 at 05:47 UTC

    A variation that doesn't require foreknowledge of the backward step length:

    c:\@Work\Perl\monks>perl -wMstrict -le "$_ = '222,345,222,678,222,223,224,543,111,222'; ;; while ( /22[2345],(\d\d\d)/g ) { print qq{after 22n: '$1'}; pos = $-[1]; } " after 22n: '345' after 22n: '678' after 22n: '223' after 22n: '224' after 22n: '543'


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