描述
您可以創建幾個捕獲組。一個用於整場比賽,第二個用於下屬比賽。當然,這種方法確實存在侷限性,可能會陷入一些非常複雜的邊緣情況。
(\[shortcode_1\s[^\]]*].*?(\[shortcode_2\s.*?\[\/shortcode_2\]).*?\[\/shortcode_1\])
例子
現場演示
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
----------------------------------------------------------------------
根據正則表達式引擎,但我想看看INT組和條件/可選組... – Dilettant