2012-08-28 16 views
-3

我有一個字符串:The_3454_WITH_DAE [2011] [RUS] [HDVDRip]我想獲取[]括號內不是3454年的四位數字,請幫助我並提供一個php正則表達式的例子。PHP正則表達式得到四位數字

回答

1

以下的正則表達式應該做的伎倆

$str = 'The_3454_WITH_DAE[2011][RUS][HDVDRip]'; 
preg_match('/\[([0-9]+)\]/', $str, $matches); 
+0

這將讓任何數量的方括號中的數字。不僅僅是OP之後的四個。不是示例字符串的問題,但可能會導致其他字符串出現問題。 – Gordon

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 
     ) 
) 
+0

thans很多它幫助我 – Shark

1
preg_match("/(?<=\[)\d{4}(?=\])/", $subject, $matches); 

將匹配四位數由squar e括號。