1

我想匹配字符串中的简码,并从这里找到以下正则表达式。它工作正常。但我想了解它是如何工作的。

谁能解释一下这个正则表达式的组件以及它如何匹配简码。

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

1 回答 1

3

有一些工具可以解释正则表达式。

例如你的:

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上阅读有关断言的更多信息

于 2011-11-21T05:22:00.890 回答