RegEx如何将方括号标签与中间的管道相匹配?

preg_match_all("[[Train]]<p />[[Plane]]<p />[[Crane Sane]]<p />[[Slain (derp)|Slained]]",$regex,$out);

I want to just extract: -

[[Slain (derp)|Slained]]

This is what I have so far: -

$regex = "/\[\[.+?\\|.+?\]\]/";

Try:

$regex = "/(\[\[[^\\]]+?\\|[^\\]]+?\]\])/";
preg_match($regex,"[[Train]]<p />[[Plane]]<p />[[Crane Sane]]<p />[[Slain (derp)|Slained]]",$out);
print_r($out[1]);

http://www.ideone.com/8YisP

Your regex will start at the [[ in [[Train and then slurp all the characters up to the next pipe, which just happens to be in another [[]] construct altogether. As a first approximation, you should exclude all closing brackets before you see the pipe.

"\[\[[^]]+\\|[^[]+\]\]"

I believe this will work and only capture what you are looking for:

<?php
    $test_string = "[[Train]]<p />[[Plane]]<p />[[Crane Sane]]<p />[[Slain (derp)|Slained]]";
    $regex = "/\[\[[^[]+\|[^\]]+\]\]/";
    preg_match_all($regex,$test_string,$out);

    print_r($out);

?>