regex - php preg_match_all, returning multiple optional values when available -


example of 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 cans see, there times ( - h:mm am/pm), "med admin route: ..." , "dosage: ...", want name (versed, oxygen, etc) , if available - time (h:mm am/pm), route (intravenous, oral, etc) , dosage (20.00 mg, 2.00 l/min, etc) stored in array. i've thought i've had in past when throw different haystack @ it fails... note appears there tab instead of space between variables time-admin or admin-dosage...

luckily you, have time on hands during lunch break :)

in regex, ? after expression means accept 1 or 0 occurences. per example:

preg_match('/^(foo)?bar/', 'foobar'); // 1 preg_match('/^(foo)?bar/', 'bar');    // 1 

in case, little hard regex, feasible anyway:

preg_match_all('/med given: (?<name>[a-za-z ]+)(- (?<time>[0-9:]+ (am|pm)))?( +med admin route: (?<route>\w+))?( +dosage: (?<dosage>.*))?/', $data, $matches); 

then post-process array:

$result = array(); foreach ($matches['name'] $key => $name) {     $result = array('name'=>$name);     if (!empty($matches['time'][$key])) $result['time'] = $matches['time'][$key];     if (!empty($matches['route'][$key])) $result['route'] = $matches['route'][$key];     if (!empty($matches['dosage'][$key])) $result['dosage'] = $matches['dosage'][$key];     $results[] = $result; } print_r($results); 

this should give you:

array (     [0] => array         (             [name] => versed              [time] => 9:50 pm             [route] => intravenous             [dosage] => 20.00 mg         )     [1] => array         (             [name] => lidocaine              [time] => 9:50 pm             [route] => intravenous             [dosage] => 150.00 mg         )     [2] => array         (             [name] => succinylcholine              [time] => 9:50 pm             [route] => intravenous             [dosage] => 200.00 mg         )     [3] => array         (             [name] => oxygen              [time] => 7:23 pm             [dosage] => 2.00 l/min         )     [4] => array         (             [name] => vancomycin         )     [5] => array         (             [name] => fentanyl         )     [6] => array         (             [name] => dopamine         )     [7] => array         (             [name] => dextrose         )     [8] => array         (             [name] => gentamicin         ) ) 

the issue here "med admin route" bit. must single word (i.e.: no spaces).


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -