Jump to content

preg_match_all - Regex noob.


Jerred121

Recommended Posts

I posted this question before, but I got off topic on the previous thread and I ended up prematurely marking it solved (I hope this new thread is OK).

 

Example of the haystack:

INTERVENTIONS:
---------------------
Med Given: Versed - 9:50 PM	Med Admin Route: Intravenous	Dosage: 20.00 MG
Med Given: Lidocaine - 9:50 PM	Med Admin Route: Intravenous	Dosage: 150.00 MG
Med Given: Succinylcholine - 9:50 PM	Med Admin Route: Intravenous	Dosage: 200.00 MG
Med Given: Oxygen - 7:23 PM	Dosage: 2.00 L/MIN
Med Given: Vancomycin
Med Given: Fentanyl
Med Given: Dopamine
Med Given: Dextrose
Med Given: Gentamicin

 

As you cans see, sometimes there are times ( - H:MM AM/PM), sometimes "Med Admin Route: ..." and "Dosage: ...", I always want the name (Versed, Oxygen, etc) and if available - the time (H:MM AM/PM), route (Intravenous, Oral, etc) and dosage (20.00 MG, 2.00 L/MIN, etc) all stored in an array.  I've thought that I've had it in the past but when I throw a different haystack at it it fails...  Also note that it appears that sometimes there is a tab instead of a space between the variables like time-Admin or Admin-Dosage...

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/
Share on other sites

// example haystack
$content = <<<BLAH
Med Given: Versed - 9:50 PM   Med Admin Route: Intravenous   Dosage: 20.00 MG
Med Given: Lidocaine - 9:50 PM   Med Admin Route: Intravenous   Dosage: 150.00 MG
Med Given: Succinylcholine - 9:50 PM   Med Admin Route: Intravenous   Dosage: 200.00 MG
Med Given: Oxygen - 7:23 PM   Dosage: 2.00 L/MIN
Med Given: Vancomycin
Med Given: Fentanyl
Med Given: Dopamine
Med Given: Dextrose
Med Given: Gentamicin
BLAH;

preg_match_all('~Med Given:\s+([^\s]+)(?:\s+-\s+(.*?(?:A|P)M))?(?:\s+Med Admin Route:\s+([^\s]+))?(?:\s+Dosage:\s+(.*))?~',$content,$matches);
array_shift($matches);
$c = count($matches[0]);
for ($x=0;$x<$c;$x++) {
  $rows[$x]  =  array(
  'med'    => trim($matches[0][$x]),
  'time'   => trim($matches[1][$x]),
  'route'  => trim($matches[2][$x]),
  'dosage' => trim($matches[3][$x]));
}

// output		
echo "<pre>";
print_r($rows);

 

output:

Array
(
    [0] => Array
        (
            [med] => Versed
            [time] => 9:50 PM
            [route] => Intravenous
            [dosage] => 20.00 MG
        )

    [1] => Array
        (
            [med] => Lidocaine
            [time] => 9:50 PM
            [route] => Intravenous
            [dosage] => 150.00 MG
        )

    [2] => Array
        (
            [med] => Succinylcholine
            [time] => 9:50 PM
            [route] => Intravenous
            [dosage] => 200.00 MG
        )

    [3] => Array
        (
            [med] => Oxygen
            [time] => 7:23 PM
            [route] => 
            [dosage] => 2.00 L/MIN
        )

    [4] => Array
        (
            [med] => Vancomycin
            [time] => 
            [route] => 
            [dosage] => 
        )

    [5] => Array
        (
            [med] => Fentanyl
            [time] => 
            [route] => 
            [dosage] => 
        )

    [6] => Array
        (
            [med] => Dopamine
            [time] => 
            [route] => 
            [dosage] => 
        )

    [7] => Array
        (
            [med] => Dextrose
            [time] => 
            [route] => 
            [dosage] => 
        )

    [8] => Array
        (
            [med] => Gentamicin
            [time] => 
            [route] => 
            [dosage] => 
        )

)

Dude! You are a master! It almost works perfectly, but I forgot to mention that the name and admin route could actually be more than one word (sorry I didn't really take that into consideration myself).  I would try to alter the pattern you provided, but that is way over my head.

That works, but what if there was no time or other variables like:

INTERVENTIONS:
---------------------
Med Given: Versed - 9:50 PM	Med Admin Route: Intravenous	Dosage: 20.00 MG
Med Given: Lidocaine - 9:50 PM	Med Admin Route: Intravenous	Dosage: 150.00 MG
Med Given: Succinylcholine - 9:50 PM	Med Admin Route: Nasal prongs	Dosage: 200.00 MG
Med Given: Vancomycin	Med Admin Route: Oral	Dosage: 20.00 MG
Med Given: Fentanyl twowords
Med Given: Dopamine	Dosage: 200.00 MG
Med Given: Oxygen - 7:23 PM	Dosage: 2.00 L/MIN
Med Given: Dextrose	Med Admin Route: Intravenous
Med Given: Gentamicin

That is all of the possible combos I can think of. The thing is that I can't depend on really any one thing being present (except the constants when the variable is present ie: Med Given: or Dosage:)  I don't mean to pile on - I should have though all of this through more when posting my question, but I really appreciate the help.

You know what, screw the potential hyphens this is pretty damn good enough, if you don't mind accounting for potential hyphens then I would surely use the pattern, but you have already done so much.  Seriously, thank you so much, you have have been a massive help. You should have a donate button in your sig, lol.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.