Gossamer Forum
Home : General : Perl Programming :

using a reg exp multiple times on one string

Quote Reply
using a reg exp multiple times on one string
HELP!

i have an HTML page represented as one string.

im using the following code to extract a value out of one HTML tag, and it works. this pattern should match multiple times though because the string occurs 3 times in this html page. i can't figure out how to run this regular expression repeatedly until it cannot match any more items. in other words, i need to loop it somehow and each time i need to pick up at the point in the string where i left off... or something like that...

sub stripIt
{
my $tmp_pg = $_[0];
if ( $tmp_pg =~ /blue\">(.*?)<\/td/ )
{
print "---$1--- ";
}

}


Using the HTML parser package looks difficult. If its not difficult, can someone post a quickie example to get me started?

Last edited by:

mozkill: Jun 27, 2003, 4:22 PM
Quote Reply
Re: [mozkill] using a reg exp multiple times on one string In reply to
Change:

if ( $tmp_pg =~ /blue\">(.*?)<\/td/ )

to

while ( $tmp_pg =~ /blue\">(.*?)<\/td/g )

Or more simply...

Code:
sub stripIt {

my @temp = $_[0] =~ m|/blue">(.*?)</td|g;
local $" = ' ----- ';
print "@temp" if (@temp);
}

Last edited by:

Paul: Jun 27, 2003, 4:33 PM
Quote Reply
Re: [Paul] using a reg exp multiple times on one string In reply to
thanks! that worked beautifully.

i guess i didn't think about this because its not very intuitive to think that this programming language is smart enough to know that when you say "while" that you actually want it to keep going on the same string.

my intuition was telling me that inside of a while loop, i could only access a string once and afterwards i would have to get another different string.

...and so i never thought of your solution...

... i guess this comes with experience...