If you want to grab an arbitrary amount of text around something for which you are searching, just specify it in the regex. If you want the leading and trailing, say, 100 characters...
/(.{0,100}$key.{0,100})/
Afterwards, because of the parens, your match will be in the variable $1.
| [reply] [d/l] |
Good idea, but i probably should have explained myself better. I need to filter out HTML as i read in the keyword. Something like this.... get keyword, go back some spaces, filter out the html and other keywords so its plain text, then go ahead of the word a bit and do the same.
| [reply] |
Then you should probably use some HTML parser class or other (HTML::Parser comes to mind), strip out all of the markup, and then use my previous suggestion. It should just be a matter of plugging together two modular solutions to solve a third problem that is really just two problems rolled into one.
| [reply] |
++skyknight!
Just an unrelated remark: if you're really searching for words rather than strings, don't forget to put word boundary markers in there:
/(.{0,100}\b$key\b.{0,100})/
Otherwise you could get some strange matches...
dave
| [reply] [d/l] |
Good point, though it would depend on the application. If you search on the word "house" and you're expecting that it will find both "house" and "houses", then you don't want the word boundary anchors, but if you're expecting to find the word exactly as you typed it, not as a substring of some pluralized word or whatnot, you are most certainly right. ++
| [reply] |
I speak from bitter experience, believe me. My day job is translating. Once upon a time, in a company that shall remain nameless where I was working, the helpful IT department came up, at my request, with an automated template translator into French (to avoid, in theory, our having to translate the exact same stuff every time we worked on a certain type of document: dates (January -> janvier), fields such as 'originated by', 'approved by', etc.):
Approved by Paul Newby
Approuvé par Paul Newpar
Oh how we laughed.
dave | [reply] [d/l] |
Show us more of your program. How are you reading lines from the file?
| [reply] |
Well ill give you guys the whole function im using
sub finder
{
@text = "";
$wordMatchFilePosition;
$number = 0;
$currentFilePosition;
$lastPushed = space;
$tagCount = 0;
$quoteCount = 0;
$loopcount = 0;
if (!-d $File::Find::name)
{
open FILEHANDLE, "<$File::Find::name";
$size = -s $File::Find::name;
$_ = '';
read FILEHANDLE, $_, $size;
#on our first match, store the text behind and after the
#word so the user can see a short desciption of why the
#word appeared
$file = $File::Find::name;
if(!excludedDirectory())
{
if(m/$key/i)
{
$wordMatchFilePosition = tell FILEHANDLE;
seek FILEHANDLE, -1700, 1;
$currentFilePosition = tell FILEHANDLE;
for($loopcount = 0; $loopcount < 3000 && $currentFilePosition
+!= $wordMatchFilePosition+700; $loopcount++)
{
$tempText = getc FILEHANDLE;
#if we find the begining of the tag, increment the counter
#dont add the text until we are back to 0 on the counter
#(meaning were not in HTML commands)
if($tempText eq "<")
{
$tagCount++;
}
#because were moving back an arbitrary number of character
+s,
#we might land in the middle of an HTML statement, therefo
+re
#if we hit a closing tag without having a begining tag
#it should be safe to just ignore it
elsif($tempText eq ">" && $tagCount > 0)
{
$tagCount--;
}
#More filtering of HTML
elsif($tempText eq " \" ")
{
if($quoteCount == 1)
{
$quoteCount = 0;
}
else
{
$quoteCount = 1;
}
}
#make sure were not reading HTML commands, the thing were
+getting
#is comprised of only letters, and were not trying to read
+ at the end
#of the file, if its a space, and the last thing we pushed
+ on the text
#was a letter, then its safe to assume that was the end of
+ a word
#we are also filtering out numbers, because they are
+ used more with
#defining pages then the actual content, we also dont want
+ things with _
#in it because its used for naming frames
elsif($tagCount == 0 && $quoteCount == 0 && !eof FILEHAND
+LE &&
$tempText =~ /(\b[A-Za-z]+\b)/ || $tempText =~ /^\s/
+&& $tempText ne "_")
{
if($lastPushed eq "character" && $tempText =~ /^\s/)
{
push(@text," ");
$lastPushed = "space";
}
else
{
push(@text,$tempText);
$currentFilePosition++;
$lastPushed = "character";
}
}
}
print @text;
print "\n";
}
$number++ while/^${key}/g;
close FILEHANDLE;
$file = $File::Find::name;
$length = length ($base_address) + 1;
$file =~ /^.{${length}}(.*)/;
if ($number) {$hash{$1} = $number}
}#end if(!excludedDirectory())
}
}
Before i get into explaining, i looked at the HTML parser, and it was a bit more complicated then i was willing to invest time in. I just made my own, and it seems to catch most of the html.
So what i do is call this subroutine and finds the match. Which works, im sure of it. Then i just run around that match taking off byte by byte and making sure that its not HTML. Then i just put it into an array, if i found a space insert it only if the last character i inserted with a letter. The result is something like this:
joint venture The Chinese government certification process for the joint venture and the new company name should be completed later this summer
its not perfect, but its "good enough" for what i want to do, and thats show people what they will see when they click on that link. | [reply] [d/l] |
Ah, OK. First you're getting the size of the file with the -s test. Then you're reading in the entire file with read---now the file position is at the very end of the file. Now, when you say "seek back 1700 characters from the current position", you're saying 1700 characters from the end of the file, since the file position is at the end of the file.
I think you want to use the pos function, which will tell you where in the string the last match happened. Since you've read the entire file, the positions in the string will be equal to the positions in the file, so you can just seek to the position that pos returns.
As others have suggested, though, you should consider using an HTML parser. HTML parsing is hard, and other people have already done it.
| [reply] [d/l] [select] |