1 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個贊
目前您只匹配一行。
您可以使用重復(fù)模式來匹配所有不以 #chapter 開頭的行,使用負(fù)前瞻:
#Chapter\h+\d+:\h+title\h+\d+\K(?:\R(?!#chapter).*)*
解釋
#Chapter
字面匹配\h+\d+:
匹配 1+ 個水平空白字符、1+ 個數(shù)字和:
\h+title\h+\d+
匹配 匹配 1+ 個水平空白字符,title
匹配 1+ 個水平空白字符和 1+ 個數(shù)字\K
忘記匹配的內(nèi)容(?:
非捕獲組\R(?!#chapter).*
匹配unicode換行序列并斷言直接在右邊的不是#chapter)*
關(guān)閉捕獲組并重復(fù) 0+ 次
例如:
preg_match_all('/#Chapter\h+\d+:\h+title\h+\d+\K(?:\R(?!#chapter).*)*/i',$str, $match);
print_r($match[0]);
結(jié)果
Array
(
[0] =>
content chapter 1 line 1
content chapter 1 line 2
content chapter 1 line 3
content chapter 1 line 4
[1] =>
content chapter 2 line 1
)
- 1 回答
- 0 關(guān)注
- 142 瀏覽
添加回答
舉報