【问题标题】:Matching wordpress shortcodes inside a given string在给定字符串中匹配 wordpress 简码
【发布时间】:2011-11-21 04:20:10
【问题描述】:
我想匹配字符串中的短代码并从这里找到以下正则表达式。它工作正常。但我想了解它是如何工作的。
谁能解释一下这个正则表达式的组件以及它如何匹配短代码。
preg_match_all('%(?<=\[shortcode\]).*?(?=\[/shortcode\])%s',$content, $result, PREG_PATTERN_ORDER);
【问题讨论】:
标签:
regex
string
wordpress
shortcode
【解决方案1】:
有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上阅读有关断言的更多信息