説明
キャプチャグループを2つ作成するだけです。 1つはマッチ全体、もう1つは従属マッチです。もちろん、このアプローチには限界があり、かなり複雑なエッジケースでハングアップする可能性があります。
(\[shortcode_1\s[^\]]*].*?(\[shortcode_2\s.*?\[\/shortcode_2\]).*?\[\/shortcode_1\])
![Regular expression visualization](https://www.debuggex.com/i/lM2Y70Pyjh2glYrZ.png)
例
ライブデモ
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