Another way would be to match the portion before it. For example:
my $point = 15; if ($str =~ /^(.{15})/ && $1 =~ /(regex)$/) { print "stuff before pos 15: $1\n"; }
This way, you at least don't have to copy the full string before searching. You only copy as much as length($1).

If you know the approximate length of the string that will be matched, you can minimize what's copied even further by using substr():
my $point = 15; my $start = 10; if (substr($str, $start, $point - $start) =~ /(regex)$/) { print "stuff before pos: $1\n"; }
If you only need to know whether or not something matched, and not actually fetch the resulting match, you can use one of Perl 5.005's look-behind assertions:
pos $str = 15; print "matched\n" if $str =~ /\G(?<=regex)/g;
There are probably better ways to deal with this, but if you post an example string, a heuristic might arise that eliminates the need to do any of this.

In reply to Re: Backwards searching with regexps by jerji
in thread Backwards searching with regexps by Anonymous Monk

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.