• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

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!
0
LordOfPorts
Asked:
LordOfPorts
  • 4
  • 2
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
LordOfPortsAuthor Commented:
Thank you very much, I really appreciate it!
0
 
ddrudikCommented:
Thanks for the question and the points.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now