2011-11-21 19 views
1

我想匹配字符串內的短碼,並從這裏找到以下正則表達式。它工作正常。但我想了解它是如何工作的。匹配給定字符串內的wordpress短碼

任何人都可以解釋我這個正則表達式的組件和它如何匹配短代碼。

preg_match_all('%(?<=\[shortcode\]).*?(?=\[/shortcode\])%s',$content, $result, PREG_PATTERN_ORDER); 
+0

[識別使用正則表達式的wordpress的簡]的可能重複(http://stackoverflow.com/questions/8207098/識別wordpress-shortcodes-using-a-regular-expression) – mario

回答

3

有正則表達式tools to explain

此致例如:

NODE      EXPLANATION 
---------------------------------------------------------------------- 
    (?<=      look behind to see if there is: 
---------------------------------------------------------------------- 
    \[      '[' 
---------------------------------------------------------------------- 
    shortcode    'shortcode' 
---------------------------------------------------------------------- 
    \]      ']' 
---------------------------------------------------------------------- 
)      end of look-behind 
---------------------------------------------------------------------- 
    .*?      any character (0 or more times 
          (matching the least amount possible) 
---------------------------------------------------------------------- 
    (?=      look ahead to see if there is: 
---------------------------------------------------------------------- 
    \[      '[' 
---------------------------------------------------------------------- 
    /shortcode    '/shortcode' 
---------------------------------------------------------------------- 
    \]      ']' 
---------------------------------------------------------------------- 
)      end of look-ahead 
---------------------------------------------------------------------- 

瞭解更多有關的斷言上http://www.regular-expressions.info/lookaround.html

+0

感謝您的有用信息 –