簡體   English   中英

PHP regexp替換除第一個以外的所有匹配項

[英]PHP regexp replace all matches except the first one

我正在使用preg_replace從以下字符串中用\\ n#替換

#0 F:\dev\htdocs\cms\lib\Cms.php(76): 
ReflectionClass->__construct('Mod_Simple_Top_...') 
#1 F:\dev\htdocs\cms\cms.php(45): Cms::renderModules(Array, 
#2 F:\dev\htdocs\cms\bootstrap.php(63): require_once('F:\dev\htdocs\c...')
#3 {main}

preg_replace('/#/','\\ n',$ error_message);

問題在於它也替換了第一個匹配項,即; #0 regexp中有任何語法,因此它不應該考慮第一個匹配項。

“查找后跟一個‘0’所有的hash符號”。

echo preg_replace('/(#)(?!0)/' , '\n', $str);

→現場例子

特別是,組(?!0)被稱為否定超前

更換后,只需先移除\\ n。 使用substr($string, 1)trim($string)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM