この部分は、あなたが思うようには機能しません:
[^\[if]
[
これは、 、i
またはのいずれでもない単一の文字に一致しf
ます。組み合わせは問いません。ただし、否定的な先読みを使用して、目的の動作を模倣できます。
~\[if-([a-z0-9-]*)\]((?:(?!\[/?if).)*)\[/if\]~s
また、ルックアヘッドに終了タグを含めました。これにより、貪欲な繰り返し (通常はパフォーマンスが低下します) を回避できます。さらに、パターン内でスラッシュをエスケープする必要がないように、区切り記号を変更しました。
((?:(?!\[/?if).)*)
したがって、これは説明された興味深い部分です。
( # capture the contents of the tag-pair
(?: # start a non-capturing group (the ?: are just a performance
# optimization). this group represents a single "allowed" character
(?! # negative lookahead - makes sure that the next character does not mark
# the start of either [if or [/if (the negative lookahead will cause
# the entire pattern to fail if its contents match)
\[/?if
# match [if or [/if
) # end of lookahead
. # consume/match any single character
)* # end of group - repeat 0 or more times
) # end of capturing group