use Modern::Perl '2014'; my $first = qr/STRING/; my $second = qr/MAGIC/; my $first_pos; my $first_sub_pos; my $second_pos; my $second_sub_pos; while () { do { $first_pos = $.; $first_sub_pos = $-[0] } if /$first/; do { $second_pos = $.; $second_sub_pos = $-[0] } if /$second/; } if ( $first_pos and $second_pos > $first_pos ) { say "Success! STRING at $first_pos followed by MAGIC at $second_pos"; } elsif ( $first_pos and $second_pos == $first_pos and $second_sub_pos > $first_sub_pos ) { say "Success! STRING (char: $first_sub_pos) followed by MAGIC (char: $second_sub_pos) at $first_pos"; } else { say "Failure! (STRING: $first_pos / $first_sub_pos - MAGIC: $second_pos / $second_sub_pos)"; } __DATA__ First line second line here is the STRING empty text the STRING again! followed by the MAGIC word more emptiness Oh no! the first STRING again sadness did we look in vain? Ah, STRING and MAGIC success MAGIC and STRING failed