Jump to content

REGEX pattern isn't flexible enough


Jerred121

Recommended Posts

Here is the code with the subject ($str) and the pattern that I started with - I've tried  a lot of variations but I can't get it grab all of the variables that I need, so I figured I'd post what I started with:

$str = "
Intervention: Airway-Intubation of Existing Tracheostomy Stoma successful
Intervention: CPR-Start Compressions only without Ventilation
Intervention: CPR-Start Compressions only without Ventilation - 12:30 AM successful
Intervention: Activation-Fire Rehabilitation Specialty Service/Response Team not successful
Intervention: Blood Glucose Analysis - 2:30 PM not successful
Intervention: Venous Access-Femoral Line successful";

$ptn = "/Intervention: *(.+) *-+? *(([0-2]*[0-9]+:[0-5]+[0-9] *(??:A|P)M)))* *((?:not )?successful)*/";
preg_match_all($ptn, $str, $CZprocedures);

echo"<pre>";
print_r($CZprocedures);
echo"</pre";

 

Here is the output from above:

Array
(
    [0] => Array
        (
            [0] => Intervention: Airway-
            [1] => Intervention: CPR-
            [2] => Intervention: CPR-Start Compressions only without Ventilation - 12:30 AM successful
            [3] => Intervention: Activation-
            [4] => Intervention: Blood Glucose Analysis - 2:30 PM not successful
            [5] => Intervention: Venous Access-
        )

    [1] => Array
        (
            [0] => Airway
            [1] => CPR
            [2] => CPR-Start Compressions only without Ventilation 
            [3] => Activation
            [4] => Blood Glucose Analysis 
            [5] => Venous Access
        )

    [2] => Array
        (
            [0] => 
            [1] => 
            [2] => 12:30 AM
            [3] => 
            [4] => 2:30 PM
            [5] => 
        )

    [3] => Array
        (
            [0] => 
            [1] => 
            [2] => 12:30 AM
            [3] => 
            [4] => 2:30 PM
            [5] => 
        )

    [4] => Array
        (
            [0] => 
            [1] => 
            [2] => successful
            [3] => 
            [4] => not successful
            [5] => 
        )

)

I need it to grab the name of the procedure (CPR-Start Compressions only without Ventilation, Blood Glucose Analysis, etc) then the time if there is one and then the result part at the end (((?:not )?successful)) if there is one.

Few notes:

  • The only thing I can count on being there is the name
  • The hyphens in names was screwing me up (ie: Airway-Intubation of Existing Tracheostomy Stoma would end up being: Airway)

 

Thanks for any help - it is greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/228717-regex-pattern-isnt-flexible-enough/
Share on other sites

Try

/^Intervention: ((?:\S+| (?!- \d+:\d+ [AP]M|(?:not )?successful))+)(?: - (\d+:\d+ [AP]M))?(( not)? successful)?/m

$str = "
Intervention: Airway-Intubation of Existing Tracheostomy Stoma successful
Intervention: CPR-Start Compressions only without Ventilation
Intervention: CPR-Start Compressions only without Ventilation - 12:30 AM successful
Intervention: Activation-Fire Rehabilitation Specialty Service/Response Team not successful
Intervention: Blood Glucose Analysis - 2:30 PM not successful
Intervention: Venous Access-Femoral Line successful";

$regex = '/^Intervention: ((?:\S+| (?!- \d+:\d+ [AP]M|(?:not )?successful))+)(?: - (\d+:\d+ [AP]M))?(( not)? successful)?/m';
preg_match_all($regex, $str, $matches, PREG_SET_ORDER);

$array = array();
foreach ($matches as $match) {
$array[] = array(
	"procedure" => $match[1],
	"time" => (empty($match[2]) ? null : $match[2]),
	"result" => (empty($match[3]) ? null : empty($match[4]))
);
}

Archived

This topic is now archived and is closed to further replies.

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