正则表达式可以搜索已经匹配的模式吗?

时间:2016-06-19 13:35:58

标签: php regex wordpress

假设有一个WordPress短代码内容如下 -

Some content here
[shortcode_1 attr1="val1" attr2="val2"]

    [shortcode_2 attr3="val3" attr4="val4"]

        Some text

    [/shortcode_2]

[/shortcode_1]
Some more content here

我的问题是假设我匹配短代码模式,以便我得到输出[shortcode_1] .... [/ shortcode_1]。但是我可以在同一次运行中使用相同的正则表达式模式获得[shortcode_2] ... [/ shortcode_2],还是必须使用第一次运行的输出再次运行它?

1 个答案:

答案 0 :(得分:1)

描述

您可以创建几个捕获组。一个用于整个比赛,第二个用于下属比赛。当然,这种方法确实有它的局限性,并且可能会挂在一些相当复杂的边缘情况上。

(\[shortcode_1\s[^\]]*].*?(\[shortcode_2\s.*?\[\/shortcode_2\]).*?\[\/shortcode_1\])

Regular expression visualization

实施例

现场演示

https://regex101.com/r/bQ0vV2/1

示例文字

[shortcode_1 attr1="val1" attr2="val2"]

    [shortcode_2 attr3="val3" attr4="val4"]

        Some text

    [/shortcode_2]

[/shortcode_1]

样本匹配

捕获组1获取shortcode_1 捕获组2获取shortcode_2

1.  [0-139] `[shortcode_1 attr1="val1" attr2="val2"]

    [shortcode_2 attr3="val3" attr4="val4"]

        Some text

    [/shortcode_2]

[/shortcode_1]`
2.  [45-123]    `[shortcode_2 attr3="val3" attr4="val4"]

        Some text

    [/shortcode_2]`

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \[                       '['
----------------------------------------------------------------------
    shortcode_1              'shortcode_1'
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
    [^\]]*                   any character except: '\]' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    ]                        ']'
----------------------------------------------------------------------
    .*?                      any character (0 or more times (matching
                             the least amount possible))
----------------------------------------------------------------------
    (                        group and capture to \2:
----------------------------------------------------------------------
      \[                       '['
----------------------------------------------------------------------
      shortcode_2              'shortcode_2'
----------------------------------------------------------------------
      \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
      .*?                      any character (0 or more times
                               (matching the least amount possible))
----------------------------------------------------------------------
      \[                       '['
----------------------------------------------------------------------
      \/                       '/'
----------------------------------------------------------------------
      shortcode_2              'shortcode_2'
----------------------------------------------------------------------
      \]                       ']'
----------------------------------------------------------------------
    )                        end of \2
----------------------------------------------------------------------
    .*?                      any character (0 or more times (matching
                             the least amount possible))
----------------------------------------------------------------------
    \[                       '['
----------------------------------------------------------------------
    \/                       '/'
----------------------------------------------------------------------
    shortcode_1              'shortcode_1'
----------------------------------------------------------------------
    \]                       ']'
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------