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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.