#! perl -l use strict; use warnings; my $s = 'uvXYZdabcXYZfg'; while ($s =~ /(\w\w\w)*?(XYZ)/g) { print 'Found match ', $1, $2, ' at pos: ', pos $s; } print '-----'; while ($s =~ /(abc)*?(XYZ)/g) { print 'Found match ', $1, $2, ' at pos: ', pos $s; }