Oskar Pearson on Thu, 20 Feb 2003 15:27:28 +0200 (SAST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[Linux dev] Re: Perl and matching several regex's


Hi

>  I have a situation where I'll need to match several
> regular expressions to a line to determine what type of information
> this line contains.
> 
>  Except for the obvious looping over the list of expressions until
> a match is found, is there a easier method/choice in perl which
> already does something like this??

I can't find one offhand; let me know if you do.

I normally do something like this:

my $lc = 0;
while (<>) {
	$lc++;

	# ignore comment and blank lines
	$_ =~ s/#.+//;
	$_ =~ s/\s+$//;
	next if (/^$/);

	if ($_ =~ /somepattern/) {
		&processsomepattern($_);
	} else if ($_ =~ /otherpattern/) {
		&processotherpattern($_);
	} else {
		die("Unable to interpret line $lc");
	}
}

or similar, depending on if I really have to call a function
or not.

Then I try and order the if statements in a way which
is going to be the most efficient in terms of the number
of likely matches.

Oskar
--
Oskar Pearson <oskar@xxxxxxxxxxx>
Qualica Technologies (Pty) Ltd
web: http://www.qualica.com/