Jump to content

Extracting strings, almost there - regex noob


Recommended Posts

I'm pulling data from a database that is horribly unorganized here is an example of a cell:

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
Intervention: Spinal Immobilization - 9:48 PM not successful
Intervention: Spinal Immobilization - 9:48 PM not successful
Intervention: Venous Access-Extremity - 9:49 PM successful

I'm trying to pull in the Med Given names (ie Lidocaine), times, admin route (Intravenous) (if available - this is one of my issues), dosage and Intervention names, times and success with two regex preg_match_all functions, and I can some of the time (ie: all the above example works fine) here is an example of a string that wont work:

Med Given: Oxygen - 12:59 AM	Dosage: 15.00 L/MIN
Intervention: Splinting-Basic - 7:04 PM not successful

Here is my code:

preg_match_all("#Med Given: (.+) - ([0-9]?[0-9]:[0-9][0-9] (AM|am|aM|Am|PM|pm|pM|Pm))	Med Admin Route: (.+)	Dosage: (.+)\r#U",$row['Interventions'],$meds);
preg_match_all("#Intervention: (.+) - ([0-9]?[0-9]:[0-9][0-9] (AM|am|aM|Am|PM|pm|pM|Pm)) (.+)\r#U",$row['Interventions'],$procedures);

I believe my issue is the meds aren't being grabbed is there is no Admin route (which i want to be optional) and any time there is a hyphen (-), nothing gets returned.

 

Sorry if I just laid all of that out in an incredibly confusing and stupid way... thanks!

Sorry for the second post, but for some reason I can't edit my previous post again!? Anyways, I was mistaken when i said that hyphens (-) cause the function to not return anything - first off I meant a hyphen in the name ie: Venous Access-Extremity, but for some reason that works in this string (in fact everything returns fine):

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
Intervention: Spinal Immobilization - 9:48 PM not successful
Intervention: Spinal Immobilization - 9:48 PM not successful
Intervention: Venous Access-Extremity - 9:49 PM successful
Intervention: Venous Access-Extremity - 9:49 PM successful
Intervention: Airway-Nasal - 9:49 PM not successful
Intervention: Airway-Oral - 9:49 PM not successful
Intervention: Airway-Rapid Sequence Induction - 9:53 PM not successful
Intervention: Airway-King LT Blind Insertion Airway Device - 9:56 PM successful
Intervention: 12 Lead ECG - 9:52 PM successful
Intervention: 12 Lead ECG - 10:00 PM successful
Intervention: 12 Lead ECG - 10:08 PM successful
Intervention: 12 Lead ECG - 10:14 PM successful

But this string:

INTERVENTIONS:
---------------------
Med Given: Oxygen - 7:23 PM	Dosage: 2.00 L/MIN
Intervention: Wound Care - 7:00 PM not successful
Intervention: Venous Access-Extremity - 7:22 PM successful

Only returns "Wound Care" and not the other two items.. so I'm obviously completely confused.

For the string that doesn't work, there is no "Med Admin Route" part for the first intervention (it's easy enough to make parts optional in regex, do a search) and likely no carriage return (\r) character at the end of the last one. That would explain only matching one of those three.

Thanks for the suggestions, I removed the \r because I don't think I really needed it and I tried making the Admin route optional before, but I didn't take into account that there was an extra tab that came with it. My new patterns:

Number of Procedures: ".preg_match_all("#Intervention: (.+) - ([0-9]?[0-9]:[0-9][0-9] (AM|am|aM|Am|PM|pm|pM|Pm)) (.+)#U",$row['Interventions'],$procedures);
Number of Meds: ".preg_match_all("#Med Given: (.+) - ([0-9]?[0-9]:[0-9][0-9] (AM|am|aM|Am|PM|pm|pM|Pm))(	(Med Admin Route:) (.+?))?	Dosage: (.+)#U",$row['Interventions'],$meds);

Works for all of them but I have one minor complaint/question - Now I am getting "Med Admin Route: ..." added individually to the $meds array, is there any way I can prevent this?

I started this post and then solved it before i had a chance to hit send, lol.  I case anyone cares here is the question:

 

Still having issues editing my posts... My last question isn't really a big deal, since I'm just going to be adding the values i want to a different array.  My problem now is that I'm having problems retrieving the full dosages for my meds.  Because I removing the carriage return at the end, it's only grabbing one character ie: "20.00 MG" becomes "2", when i add the carriage return (or \n) it returns the the full dosage but strips off last med (when there is only meds, not followed by anything).

 

The solution:

 

I added

."\n"

to the end of my haystack so there was always a new line ie:

preg_match_all("#Med Given: (.+) - ([0-9]?[0-9]:[0-9][0-9] (AM|am|aM|Am|PM|pm|pM|Pm))(	(Med Admin Route:) (.+?))?	Dosage: (.+)\n#U",$row['Interventions']."\n",$meds);

 

Solved.

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.