in reply to Backwards searching with regexps
This way, you at least don't have to copy the full string before searching. You only copy as much as length($1).my $point = 15; if ($str =~ /^(.{15})/ && $1 =~ /(regex)$/) { print "stuff before pos 15: $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:my $point = 15; my $start = 10; if (substr($str, $start, $point - $start) =~ /(regex)$/) { print "stuff before pos: $1\n"; }
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.pos $str = 15; print "matched\n" if $str =~ /\G(?<=regex)/g;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: Backwards searching with regexps
by Anonymous Monk on Mar 22, 2000 at 05:50 UTC | |
by chromatic (Archbishop) on Mar 22, 2000 at 23:06 UTC |