Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I find all regex matches in a string? 1

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi,
I'm not a Perl programmer, and I've done almost no Perl before, but I need to edit script at work.

I have a string that looks like this:
Code:
Blah, blah...

Some SQL statement.
DB20000I some info.

Some SQL statement.
DB21034E error text.

...
and I need to find only the errors and parse them out.

I believe this is the right regex string to search for the whole block of text (separated by blank lines) containing the error code:
Code:
m/\n\n.*\nDB\d\d\d\d\dE.*\n\n/
but if I'm not mistaken, this will only find the first match. How would I find all the matches and dump them all into a new string that I can print out?
 
You want to match two records like this?

Some SQL statement.
DB21034E error text.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yes, everything between the blank line before and after. So these 3 lines should also match:
Code:
Some SQL statement
DB21034E Error text.
Some more crap...
 
maybe:

Code:
open (IN, 'yourfile') or die "$!";
my @lines = do {local $/ = "\n\n"; <IN>};
close IN;
foreach my $record (@lines) {
   if ($record =~ /^DB\d\d\d\d\dE/m) {
      print $record;
   }
}
__DATA__
Some SQL statement.
DB20000I some info.

Some SQL statement.
DB21034E error text.
blah blah

Some SQL statement.
DB20000I some info.
foo foo foo

Some SQL statement.
DB21034E error text.

Some SQL statement.
DB20000IE some info.
blah blah blah

Some SQL statement.
DB21034E error text.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks, but I think I got it working with this:
Code:
	my $sqlErrors;
	my @db2Blocks = split /\n\n/, $returnVal;
	while ( my $db2Block = pop @db2Blocks )
	{
		if ( $db2Block =~ m/DB\d\d\d\d\dE/ )
		{
			$sqlErrors .= $db2Block . "\n\n";
		}
	}
 
Is there a reason you are using pop() to parse the array backwards? If you wanted to parse the array backwards this should be faster:

Code:
    my $sqlErrors;
    my @db2Blocks = split /\n\n/, $returnVal;
    foreach my $db2Block ( reverse @db2Blocks )
    {
        if ( $db2Block =~ m/^DB\d\d\d\d\dE/ )
        {
            $sqlErrors .= $db2Block . "\n\n";
        }
    }

Using a while() loop to process an array is slower than just looping through the array.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Wait a sec... are you saying pop() starts at the end of the array and moves toward the beginning? Doh!
I'll have to review the results of my previous test tomorrow.
 
Have you looked at the module? You're using SQL, and Class::DBI is a module that deals with SQL. I was just saying that if you're using Class::DBI, then you don't have to issue a statement and look for errors. You can just override the _croak function and you'll get all the errors. If you're not using Class::DBI, then disregard the suggestion.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top