I have a long htdoc of similar pattern which goes on like this:
<td class="MODULE_PRODUCTS_CELL " align="center" valign="top" height="100">
<table width="100" summary="products"><tr>
<td align="center" height="75">
<a href="/collections.php?prod_id=50">
<img src="files/products_categories50_t.txt" border="0" alt="products" /></a><\br>
</td>
</tr>
<tr>
<td align="center">
<a href="/collections.php?prod_id=50"><strong>Buffer</strong><br />
</a>
<td>
</tr></table>
</td>
In the above html I want to extract:
collections.php?prod_id=50
files/products_categories50_t.txt
Buffer
I have tried this code to begin with,
#!/usr/local/bin/perl
use strict;
use warnings;
my $filename = 'sr.txt';
open(FILENAME,$filename);
my @str = <FILENAME>;
chomp(@str);
#print "@str";
foreach my $str(@str){
if ($str =~/<td class(.*)<a href(.*?)><\/td>/) {
print "*****$2\n";
}
}
This code is a trial one. However it brings only last occurrence and not each occurrence. Why?
First of all, your code only reads the first line of your input. If you want to iterate through all the lines of your input, you should use this:
Assuming your input is well formated, and the href attribute always comes after the 'a' tag, and the src attribute always follows 'img' tag, and you don't have spaces in your URL's, and you don't have more than one strong tag per line, then you could use the following:
This code is quite ugly, but it does what you want. Parsing with XPath, as suggested by ptomli would be easier, and more elegant.
SUMMARY
Using patterns on little, limited pieces of reasonably well-defined pieces of HTML is quick and easy. But using them on an entire document containing fully general, open-ended HTML of unforeseeable quirks is, while theoretically possible, in practice much too hard compared with using someone else’s parser that’s already been written for that express purpose. See also this answer for a more general discussion on using patterns on XML or HTML.
Naïve Regex Solution
You’ve asked for a regex solution, so I will provide you such.
That program, when run, produces this output:
If you are quite certain that works for the particular specimen of HTML that you wish it to, then by all means use it. Notice several things that I do which you didn’t. One of them is not dealing with the HTML a line at a time. That virtually never works.
However, this sort solutions works only on extremely limited forms of valid HTML. You can only use it when you can guarantee that the HTML you’re working with really looks like what you expect it to.
The problem is that it quite often does not look all neat and tidy. For these situations, you are strongly advised to use an HTML parsing class. However, no one seems to have shown you the code to do that. That’s not very helpful.
Wizard-Level Regex Solution
And I’m going to be one of them myself. Because I am going to show you a more general solution for approaching what I believe your take to be, but unlike anyone else who ever posts on Stack Overflow, I’m going to use regexes to do it, just to show you that it can be done, but that you do not wish to do it this way:
That program, when run, produces this output:
The Choice Is Yours — Or Is It?
Both those solve your problem with regexes. It is possible that you will be able to use the first of my two approaches. I cannot say, because like seemingly all such questions asked here, you haven’t told us enough about the data for us (and perhaps also you) to know for sure whether the naïve approach will suffice.
When it doesn’t, you have two choices.
I find it unlikely that even 1 person in a 1000 would reasonably make the first of those two choices. In particular, I find it extremely unlikey that someone who asks for help with regexes as simple as those in my first solution would be a person capable of managing the regexes given in my second solution.
Which really leaves you with only one “choice” — if I may use that word so loosely.
You may find that parsing this would be easier with XPath than regexes. Your data could do with being somewhat more semantically structured though, but I guess that might be out of your hands.
Have a look at XML::XPath.
The 10-Minute XPath Tutorial from Automating System Administration with Perl also might be handy.