#!/usr/bin/env perl -l use strict; use warnings; my $search = 'AAA'; my $offset = length $search; while () { chomp; my $last_find_pos = length($_) - $offset; my $found_pos = rindex $_, $search, $last_find_pos; if ($found_pos == -1) { print "'$search' not found in '$_'"; } elsif ($found_pos == $last_find_pos) { print "'$search' found at end of '$_'"; } else { print "'$search' found before (but not at) end of '$_'"; } } __DATA__ AAAAA AAAAB AAABB AABBB ABBBB BBBBB #### 'AAA' found at end of 'AAAAA' 'AAA' found before (but not at) end of 'AAAAB' 'AAA' found before (but not at) end of 'AAABB' 'AAA' not found in 'AABBB' 'AAA' not found in 'ABBBB' 'AAA' not found in 'BBBBB'