Page 1 of 1

[SOLVED] Multiple pattern matches on the same line in perl?

Posted: Sat Aug 07, 2004 3:56 pm
by Chris Corbyn
How do I serach for multiple pattern matches ANYWHERE in a file in perl? At the moment it stops reading and moves to the next line as soon as it finds a match. I need it to carry on along the line to see if there are anymore matches.

Here's my code (it looks for hyperlinks and extracts the url in html files.

Code: Select all

#!/perl/bin/perl -w

%args = ();
$i = 0;
foreach $filenames (@ARGV) {
	$args{$i} = $filenames;
	if (-e $filenames) {
		open(DATA, "< $filenames");
		while (<DATA>) &#123;
			if (m&#123;<\s*a\s+href\s*=\s*"(&#1111;^"]+)"\s*>&#125;gi) &#123;
				print ("$1 is a link in file $filenames\n");
			&#125;
		&#125;
	&#125; else &#123;
		print ("\n\nFile: $filenames does not exist in this directory\n");
	&#125;
	++$i;
&#125;
I know I've got unused vars etc in there but it's just the beginning of a longer program. I always got the impression "g" would make the search global so is it something to do with my regexp? The regexp allows for spaces where html would forgive whitespace too. See if anyone can make this work for me :-(

Thanks :-)

Posted: Sat Aug 07, 2004 4:02 pm
by nigma
Let me play with it, i'll get back to you in a few minutes if I work things out.

Posted: Sat Aug 07, 2004 4:04 pm
by Chris Corbyn
Thanks. I was trying this all yesterday too. I'm not too good on my pattern matching but i'm pretty confused with this :?

Posted: Sat Aug 07, 2004 4:13 pm
by Chris Corbyn
my stupidity. Sorry.... I'm using "if" when I need a loop.

Changed it to....

Code: Select all

#!/perl/bin/perl -w

%args = ();
$i = 0;
foreach $filenames (@ARGV) &#123;
	$args&#123;$i&#125; = $filenames;
	if (-e $filenames) &#123;
		%links = ();
		open(DATA, "< $filenames");
		while (<DATA>) &#123;
			while (m/<\s*a\s+href\s*=\s*"(&#1111;^"]+)"\s*>/gi) &#123;
				print ("$1 is a link in file $filenames\n");
			&#125;
		&#125;
	&#125; else &#123;
		print ("\n\nFile: $filenames does not exist in this directory\n");
	&#125;
	++$i;
&#125;
It works fine now. Don't really know why it took me so long to spot that. LOL :oops:

Thanks in any case... You probably spotted it before i finish typing this.

Posted: Sat Aug 07, 2004 4:22 pm
by nigma
haha, glad you figured it out.