匹配给定字符串中的wordpress短代码

时间:2011-11-21 04:20:11

标签: regex string wordpress shortcode

我想匹配字符串中的短代码,并从此处找到以下正则表达式。它工作正常。但我想知道它是如何工作的。

任何人都可以解释这个正则表达式的组件以及它与短代码的匹配方式。

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

1 个答案:

答案 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
----------------------------------------------------------------------

详细了解http://www.regular-expressions.info/lookaround.html

上的断言