我正在研究 bbcodes(基本上是 Wordpress 短代碼)的自定義實現。為此,我需要匹配兩個類似 bbcode 的標簽之間的內容。例如:[example]The content I want to retrieve[/example]問題是這些標簽基本上可以是任何東西。這次可能是個例子,但很可能是這樣的:[hello_world with="attribute"]And some [more-complex] content[/hello_world]我唯一需要的是 hello_world 短代碼中更復雜的內容。我找到了一個實現此目的的正則表達式,我對其進行了一些修改以滿足我的需要:(?<=\[.*\])(.*?)(?=\[.*\])但是在下面的代碼中使用時:<?php$tag = '[test_tag with="attributes"]Content I [want] To capture[/test_tag]';// Get the content of the shortcode.preg_match('~(?<=\[.*\])(.*?)(?=\[.*\])~', $tag, $shortcodeContent);var_dump($shortcodeContent);我收到以下錯誤:Warning: preg_match(): Compilation failed: lookbehind assertion is not fixed length at offset 10 是否有一種簡單的方法來修復此錯誤?我知道發生這種情況是因為我使用了長度未指定的“全部捕獲”模式。但是我對如何真正解決這個問題感到困惑。(我不是真的不是正則表達式向導)
正則表達式匹配類似 bbcode 的標簽之間的內容
幕布斯6054654
2022-12-30 17:42:52