Jerred121 Posted February 1, 2011 Share Posted February 1, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/226299-extracting-strings-almost-there-regex-noob/ Share on other sites More sharing options...
Jerred121 Posted February 1, 2011 Author Share Posted February 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226299-extracting-strings-almost-there-regex-noob/#findComment-1168143 Share on other sites More sharing options...
salathe Posted February 1, 2011 Share Posted February 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226299-extracting-strings-almost-there-regex-noob/#findComment-1168282 Share on other sites More sharing options...
Jerred121 Posted February 1, 2011 Author Share Posted February 1, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/226299-extracting-strings-almost-there-regex-noob/#findComment-1168375 Share on other sites More sharing options...
Jerred121 Posted February 1, 2011 Author Share Posted February 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226299-extracting-strings-almost-there-regex-noob/#findComment-1168435 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.