searching/grepping for words "near" each other
Kevin D. Clark
kevin_d_clark at comcast.net
Sun May 3 12:16:20 EDT 2009
> That will work, but only if the search terms appear in document in the
> same order as they appear in the query. (This appears to be the case
> with the hipdig solution as well. Correct me if I'm wrong, of
> course.) The search terms I'm looking for could appear in the target
> document in any order. Perhaps I could have made that clearer. Okay,
> I *could* have made that clearer.
Tricky but doable.
#!/usr/bin/perl
# author: kevin d. clark
use warnings;
use strict;
my %seen;
my $r;
undef $/;
while (defined($_=<>)) {
undef $^N;
print "Filename: $ARGV\n$&\n" if (/
((??{
$r = "";
$seen{$^N}++ if (defined($^N));
$r .= "weapons|" if (!defined($seen{"weapons"}));
$r .= "mass|" if (!defined($seen{"mass"}));
$r .= "distraction" if (!defined($seen{"distraction"}));
$r;
}))
.{0,100}? # 0-100 characters of any random cruft
((??{
$r = "";
$seen{$^N}++ if (defined($^N));
$r .= "weapons|" if (!defined($seen{"weapons"}));
$r .= "mass|" if (!defined($seen{"mass"}));
$r .= "distraction" if (!defined($seen{"distraction"}));
$r;
}))
.{0,100}? # 0-100 characters of any random cruft
((??{
$r = "";
$seen{$^N}++ if (defined($^N));
$r .= "weapons|" if (!defined($seen{"weapons"}));
$r .= "mass|" if (!defined($seen{"mass"}));
$r .= "distraction" if (!defined($seen{"distraction"}));
$r;
}))
/xs)
}
__END__
I could generalize this and all, but I am busy. Basically, the gist
of this code is that it generates the regexp to match while the regexp
engine is doing the matching.
Just another Perl hacker,
--kevin
--
GnuPG ID: B280F24E God, I loved that Pontiac.
alumni.unh.edu!kdc -- Tom Waits
http://kdc-blog.blogspot.com/
More information about the gnhlug-discuss
mailing list