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

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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 :-)
Last edited by Chris Corbyn on Sat Aug 07, 2004 7:51 pm, edited 1 time in total.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Let me play with it, i'll get back to you in a few minutes if I work things out.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

haha, glad you figured it out.
Post Reply