我想匹配字符串中的短代码,并从此处找到以下正则表达式。它工作正常。但我想知道它是如何工作的。
任何人都可以解释这个正则表达式的组件以及它与短代码的匹配方式。
preg_match_all('%(?<=\[shortcode\]).*?(?=\[/shortcode\])%s',$content, $result, PREG_PATTERN_ORDER);
答案 0 :(得分: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
----------------------------------------------------------------------
上的断言