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


In reply to Re: How do I match a string that is NOT at the end of a line? by kcott
in thread How do I match a string that is NOT at the end of a line? by davidfilmer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.