views:

63

answers:

1

UPDATE: I'm making progress, but this is hard!

The test text will be valid[REGEX_EMAIL|REGEX_PASSWORD|REGEX_TEST].

(The real life text is required|valid[REGEX_EMAIL]|confirmed[emailconfirmation]|correct[not in|emailconfirmation|email confirmation].)

([^|]+) saves REGEX_EMAIL, REGEX_PASSWORD and REGEX_TEST in an array.

^[^[]+\[ matches valid[

\] matches ]

^[^[]+\[ + ([^|]+) + \] doesn't save REGEX_EMAIL, REGEX_PASSWORD and REGEX_TEST in an array.

How to solve?

+1  A: 

Why is it important to try to everything with a single regular expression? It becomes much easier if you extract the two parts first and then split the strings on | using explode:

$s = 'valid[REGEX_EMAIL|REGEX_PASSWORD|REGEX_TEST]';
$matches = array();
$s = preg_match('/^([^[]++)\[([^]]++)\]$/', $s, $matches);
$left = explode('|', $matches[1]);
$right = explode('|', $matches[2]);
print_r($left);
print_r($right);

Output:

Array
(
    [0] => valid
)
Array
(
    [0] => REGEX_EMAIL
    [1] => REGEX_PASSWORD
    [2] => REGEX_TEST
)
Mark Byers
Please, come back! The question was updated.
Delirium tremens
I've updated my answer.
Mark Byers
it's really like you've said it! `(?:c(a)t)+` for catcat saves the second a only :-( ++ is too advanced, I still don't understand atomic grouping :-(
Delirium tremens
You can replace ++ with + and it will give you the exact same result.
Mark Byers