LordOfPorts
asked on
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 AAAA0000000000000000000012 34 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 AAAA0000000000000000000045 67 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 AAAA0000000000000000000012 34, which is known, and another id which in the example above is AAAA0000000000000000000045 67 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 = '/AAAA00000000000000000000 1234/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!
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 AAAA0000000000000000000012
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 AAAA0000000000000000000045
Using perl_match_all or another function I would like to extract every line that contains the word exception between the id AAAA0000000000000000000012
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 = '/AAAA00000000000000000000
$aMatches = array();
if(preg_match_all($regExp,
foreach ($aMatches as $sMatch) {
echo $sMatch.'<br />';
}
}
Thank you very much for any advice you might have and your time!
Do you want to do something with the "Line of text with value AAAA0000000000000000000012 34 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.
Also, is this multi-line string read from a text file?
ASKER
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 AAAA0000000000000000000012 34, 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.
$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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much, I really appreciate it!
Thanks for the question and the points.