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
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] => 
        )

)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.