-3

私は文字列を持っています:The_3454_WITH_DAE [2011] [RUS] [HDVDRip]そして私は3454ではなく[]括弧の間の年の4桁を取得したいのですが、私を助けて正規表現のphpの例を提供してください。

4

3 に答える 3

3

それらを一致させるには、角かっこをエスケープする必要があります

\[([\d]{4})\]

デモhttp://codepad.viper-7.com/J4Rnkt

preg_match_all(
    '/
        \[           # match any opening square bracket
        ([\d]{4})    # capture the four digits within
        \]           # followed by a closing square bracket
    /x', 
    'The_3454_WITH_DAE[2011][RUS][HDVDRip]',
    $matches
);

print_r($matches);

出力:

Array
(
    [0] => Array
        (
            [0] => [2011]
        )

    [1] => Array
        (
            [0] => 2011
        )
)
于 2012-08-28T13:10:09.823 に答える
1
preg_match("/(?<=\[)\d{4}(?=\])/", $subject, $matches);

角かっこで囲まれている場合、4桁に一致します。

于 2012-08-28T13:10:48.140 に答える
1

次の正規表現でうまくいくはずです

$str = 'The_3454_WITH_DAE[2011][RUS][HDVDRip]';
preg_match('/\[([0-9]+)\]/', $str, $matches);
于 2012-08-28T13:11:54.973 に答える