Regular Expression in PHP Advice Inquiry

Dear Experts,

I would like to ask for advice regarding a regular expression (Perl-compatible); I have a multi-line string in the following format:

Line of text with value AAAA000000000000000000001234 followed by some text
Line of other text
Line of other text
Line of text with the word exception
Line of text with the word java.lang.SomeException
Line of other text
Line of text with value AAAA000000000000000000004567 followed by some text

Using perl_match_all or another function I would like to extract every line that contains the word exception between the id AAAA000000000000000000001234, which is known, and another id which in the example above is AAAA000000000000000000004567 and not known so. The number is a sequence of 4 letters followed by 24 digits. The extracted text in the example above would be:

Line of text with the word exception
Line of text with the word java.lang.SomeException

The sequence I am hoping for would approximately be:

$regExp = '/AAAA000000000000000000001234/im'; // <- need regular expression

$aMatches = array();

if(preg_match_all($regExp, $sText, $aMatches) == 1) {
    foreach ($aMatches as $sMatch) {
        echo $sMatch.'<br />';
    }
}

Thank you very much for any advice you might have and your time!
LVL 19
LordOfPortsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddrudikCommented:
Do you want to do something with the "Line of text with value AAAA000000000000000000001234 followed by some text" lines or is it just finding the lines with exceptions?  Possibly show what you want to resulting array to appear as for your example.
0
ddrudikCommented:
Also, is this multi-line string read from a text file?
0
LordOfPortsAuthor Commented:
I would only have to extract the lines of text that contain the word exception and are located between the known id, which in the example above is AAAA000000000000000000001234, and the next id which not known or possibly not even present if the known id is the last in the file, in the example above the extracted text would be in the array in the following manner:

$aMatches[0] = 'Line of text with the word exception';
$aMatches[1]  = 'Line of text with the word java.lang.SomeException';

The multi-line string is indeed read from a text file through the fread function; the  $sText variable contains the entire contents of the file afterwards.
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

ddrudikCommented:
When calling the function you could just call file_get_contents($yourpath) if you don't have the file read into a string or you could always do that within the function if you don't otherwise need the log file text elsewhere.
<pre>
<?php
$str='Line of text with value AAAA000000000000000000001234 followed by some text
Line of other text
Line of other text
Line of text with the word exception
Line of text with the word java.lang.SomeException
Line of other text
Line of text with value AAAA000000000000000000004567 followed by some text
Line of other text
Line of other text
Line of other text';
function geterrors($needle,$haystack){
	if(preg_match('/[^\r\n]*'.preg_quote($needle).'[^\r\n]*(.*?)(?=\r\n[a-z]{4}\d{24}|$)/is',$haystack,$match)){
		$errcount=preg_match_all('/^.*exception.*/im',$match[1],$errors);
		if($errcount>0){
			$errors=$errors[0];
		} else {
			$errors="no errors found for '$needle'.";
		}
	} else {
		$errors="'$needle' not found.";
	}
	return $errors;
}
echo print_r(geterrors('AAAA000000000000000000001234',$str),true);
echo '<br>';
echo print_r(geterrors('AAAA000000000000000000004567',$str),true);
echo '<br>';
echo print_r(geterrors('AAAB000000000000000000004567',$str),true);
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LordOfPortsAuthor Commented:
Thank you very much, I really appreciate it!
0
ddrudikCommented:
Thanks for the question and the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.