Link to home
Start Free TrialLog in
Avatar of LordOfPorts
LordOfPortsFlag for United States of America

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 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!
Avatar of ddrudik
ddrudik
Flag of United States of America image

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.
Also, is this multi-line string read from a text file?
Avatar of LordOfPorts

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 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.
ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much, I really appreciate it!
Thanks for the question and the points.