in reply to How do I match a string that is NOT at the end of a line?
G'day davidfilmer,
If you know the string you're looking for, which your question suggests, then there's no need to fire up the regex engine. The function rindex will find the position of the last occurence of the string in the line.
Take a look at this script which will tell you if the string wasn't found in the line; was found at the end of the line; or was found before, but not at, the end of the line.
#!/usr/bin/env perl -l use strict; use warnings; my $search = 'AAA'; my $offset = length $search; while (<DATA>) { 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
Output:
'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'
-- Ken
|
|---|