Jerred121 Posted February 9, 2011 Share Posted February 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/ Share on other sites More sharing options...
.josh Posted February 9, 2011 Share Posted February 9, 2011 // 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] => ) ) Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/#findComment-1171916 Share on other sites More sharing options...
Jerred121 Posted February 9, 2011 Author Share Posted February 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/#findComment-1171921 Share on other sites More sharing options...
.josh Posted February 9, 2011 Share Posted February 9, 2011 preg_match_all('~Med Given: ((??!-|$).)+)(?:\s*-\s*(.*?(?:A|P)M))?(?:\s*Med Admin Route:((??!Dosage:|$).)+))?(?:\s*Dosage:\s*(.*))?~',$content,$matches); Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/#findComment-1171929 Share on other sites More sharing options...
Jerred121 Posted February 9, 2011 Author Share Posted February 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/#findComment-1171933 Share on other sites More sharing options...
.josh Posted February 9, 2011 Share Posted February 9, 2011 what about being in different orders? Like... Dosage: xxx Med Admin Route: xxx Med Given: xxx Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/#findComment-1171936 Share on other sites More sharing options...
Jerred121 Posted February 9, 2011 Author Share Posted February 9, 2011 That shouldn't happen, if it wouldn't be much trouble to incorporate that just to be on the safe side, sure, but I doubt the script would ever see that so it's not a big deal. Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/#findComment-1171938 Share on other sites More sharing options...
.josh Posted February 9, 2011 Share Posted February 9, 2011 preg_match_all('~Med Given: ((??!-|Med Admin Route:|Dosage:|$).)+)(?:\s*-\s*(.*?(?:A|P)M))?(?:\s*Med Admin Route:((??!Dosage:|$).)+))?(?:\s*Dosage:\s*(.*))?~',$content,$matches); Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/#findComment-1171946 Share on other sites More sharing options...
Jerred121 Posted February 9, 2011 Author Share Posted February 9, 2011 Also names may have hyphens (-) or other symbols, this just keeps getting harder! Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/#findComment-1171948 Share on other sites More sharing options...
Jerred121 Posted February 9, 2011 Author Share Posted February 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/#findComment-1171951 Share on other sites More sharing options...
.josh Posted February 9, 2011 Share Posted February 9, 2011 I think this should cover hyphens preg_match_all('~Med Given: ((??!-\s*\d{1,2}:\d{1,2} (?:A|P)M|Med Admin Route:|Dosage:|$).)+)(?:\s*-\s*(.*?(?:A|P)M))?(?:\s*Med Admin Route:((??!Dosage:|$).)+))?(?:\s*Dosage:\s*(.*))?~',$content,$matches); Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/#findComment-1171962 Share on other sites More sharing options...
Jerred121 Posted February 9, 2011 Author Share Posted February 9, 2011 Perfect! I think you have it all covered it all... +1,000,000 Quote Link to comment https://forums.phpfreaks.com/topic/227181-preg_match_all-regex-noob/#findComment-1171965 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.