[英]PHP regEx help needed with /*<##> </##>*/
我在這里指出了兩個問題,您沒有捕獲組來替換替換調用中的定界標記,並且Negative Lookahead語法缺少重復運算符 。
$text = preg_replace('%(/\*<##>)(?:(?!\*/).)*(</##>*\*/)%s', '$1new$2', $text);
雖然,您可以用.*?
代替前瞻.*?
因為您正在使用s
(dotall)修飾符。
$text = preg_replace('%(/\*<##>).*?(</##>*\*/)%s', '$1new$2', $text);
或者考慮結合使用環顧四周方法來執行此操作而不捕獲組。
$text = preg_replace('%/\*<##>\K.*?(?=</##>\*/)%s', 'new', $text);
經過測試:
$input = "something /*<##>old or something else</##>*/ something other";
echo preg_replace('%(/\*<##>)(.*)(</##>\*/)%', '$1new$3', $input);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.