use strict; my $target = shift || 'word'; my $re = qr/a=<(.*?$target.*?)>/; while( ) { if (index($_, $target) >= 0 && /$re/) { #short-circuit regex! print "[$1]\n"; } }