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
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]))
);
}

Link to comment
Share on other sites

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.