my $re = qr/(x(.*?)x(??{ '.{'.length($2).'}' })x)/; my $str = "ax1x2xbx34x56xc"; while ($str=~/$re/g) { print "found \"$1\"\n"; } __END__ found "x1x2x" found "x34x56x"