匹配给定字符串内的 WordPress 短代码

发布于 2024-12-17 07:16:16 字数 220 浏览 4 评论 0原文

我想匹配字符串内的短代码,并从这里找到了以下正则表达式。效果很好。但我想了解它是如何工作的。

谁能解释一下这个正则表达式的组成部分以及它如何匹配短代码。

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

I wanted to match shortcodes inside a string and found the following regex from here. It works fine. But i want to learn how it works.

Can anyone plz explain me the components of this regex and how it matches the shortcode.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡大本事 2024-12-24 07:16:16

解释正则表达式的工具

例如,您的:

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

There are tools to explain regular expressions.

Yours for example:

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

Read more about the assertions on http://www.regular-expressions.info/lookaround.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文