独自一人,正则表达式的一部分。 如果在它之前添加了一个零件,在它之后添加了另一个零件,它将停止工

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?

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
)