in reply to Re: Find a specific word in a text file
in thread Find a specific word in a text file

OK Dave now it works: Found at 5477. Now I need to move some blank spaces and 4 letters forward to grab five digits. Any ideas?
  • Comment on Re^2: Find a specific word in a text file

Replies are listed 'Best First'.
Re^3: Find a specific word in a text file
by wfsp (Abbot) on Sep 09, 2004 at 09:34 UTC
    davido has given you good advice. I would go further and say _don't_ parse HTML even in apparently straightforward cases.

    There is often (always?) shed loads of arbitray white space which can easily defeat a regex. The HTML can be 'loose', 'strict' and change every day!

    Once you've used HTML::TokeParser (if I can, anybody can!) you'll be able to reuse the code in any future apps.

    Have a look at this tutorial. There is an example here. Search and Supersearch will find many more.

    It does seem like a lot of trouble if you are in a hurry! But I assure you the effort will pay dividends.

    Best of luck, wfsp

      Thanks. I did just that (I used tokeparser). It took a while but its worth it. Thx.
Re^3: Find a specific word in a text file
by davido (Cardinal) on Sep 09, 2004 at 15:21 UTC

    I wish you had just gone ahead and asked the whole question at once as we requested. Breaking a single question into pieces and only feeding us one piece at a time might seem to be a good approach to you, but trust me, we can take bigger bites. We don't want to write your script for you, but if we're going to answer a question, at least let us answer the complete question.

    You really should be using HTML::TokeParser. Nevertheless, the following will use a fragile regexp to find a keyword, and grab the digits that immediately follow it (whitespace optional).

    if( $page =~ m/keyword\s+(\d+)/ ) { print "Found the keyword, and retrieved a value of $1\n"; }

    Now if your HTML has multiple instances of this keyword, you'll have to ask us another question, or read perlretut and perlrequick.

    By the way, if you're screen-scraping Google you are violating their Terms of Service, and exposing yourself to civil liability. From the Google Terms of Service page:

    No Automated Querying
    You may not send automated queries of any sort to Google's system without express permission in advance from Google. Note that "sending automated queries" includes, among other things:

    • using any software which sends queries to Google to determine how a website or webpage "ranks" on Google for various queries;
    • "meta-searching" Google; and
    • performing "offline" searches on Google.

    Please do not write to Google to request permission to "meta-search" Google for a research project, as such requests will not be granted.


    Dave

Re^3: Find a specific word in a text file
by Anonymous Monk on Sep 09, 2004 at 14:36 UTC
    Unless I'm mistaken, it appears that what you're trying to do can be solved with a fairly simple regular expression. Unless you actually need the index, try:
    if($page =~ m/\bword\b\s+.{4}(\d{5})/) { print "The number is $1"; } else { print "No match"; }
    From left-to-right, the expression states:

    \bword\b - Find the text 'word'
    \s+ - Followed by one or more whitespace characters
    .{4} - Followed by any four characters
    (\d{5}) - Followed by five digits

    The parenthesis around the '\d{5}' instruct perl to store this match in the variable $1 (for the purposes of this discussion). So the matched digits are stored in $1.