Q:

Regex needed to extract particular values

I am struggling to determine the regexp to use with preg_match to return an array for the following string:
key_people = Mark Zuckerberg
([[Chairman]] and [[CEO]])
[[Sheryl Sandberg]]
([[Chief operating officer|COO]])

The array I am looking for would be similar to the following:
array(2) {
["Chairman and CEO"]=> "Mark Zuckerberg"
["Chief operating officer"]=> "Sheryl Sandberg"
}

Any assistance is much appreciated. 

A:

It's not perfect but here's what I did:
$string = "key_people = Mark Zuckerberg ([[Chairman]] and [[CEO]]) [[Sheryl Sandberg]] ([[Chief operating officer|COO]])";

// key_people obviously is a variable, and you only need it's vaue
$string = substr($string, strpos($string, " = "), strlen($string));

preg_match_all('/[^\)]+/', $string, $matches);

$normalized_array = array();
foreach($matches[0] as $index => $match) {
    $k = strrpos($match, "(") + 1;

    $person = substr($match, 0, $k - 1);
    $person = trim_linked_resources($person);

    $job_positions_raw = substr($match, $k, strlen($match));
    $job_positions = parse_linked_resources($job_positions_raw);

    $normalized_array[$index] = array(
        "person" => $person,
        "job_positions" => $job_positions,
        "job_positions_as_text" =>implode(" and ", $job_positions)
    );
}

print_r($normalized_array);

// The resources surrounded by [[]] we assume are linked resources.
function parse_linked_resources($string) {
    preg_match_all('/\[\[([^\]]+)\]\]/', $string, $matches);
    return $matches[1];
}

function trim_linked_resources($string) {
    $string = str_replace("[", "", $string);
    $string = str_replace("]", "", $string);    
    return $string;
}

The output is as follows:
Array
(
    [0] => Array
        (
            [person] =>  = Mark Zuckerberg 
            [job_positions] => Array
                (
                    [0] => Chairman
                    [1] => CEO
                )

            [job_positions_as_text] => Chairman and CEO
        )

    [1] => Array
        (
            [person] =>  Sheryl Sandberg 
            [job_positions] => Array
                (
                    [0] => Chief operating officer|COO
                )

            [job_positions_as_text] => Chief operating officer|COO
        )

)

It could be improved further but hope this will work for your case.

