亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我有 preg_match_all 函數的問題

我有 preg_match_all 函數的問題

PHP
慕標琳琳 2021-08-28 18:30:11
我不知道如何從此代碼中獲取全行內容。$str = '#chapter 1: title 1content chapter 1 line 1content chapter 1 line 2content chapter 1 line 3content chapter 1 line 4#chapter 2: title 2content chapter 2 line 1';preg_match_all('/#Chapter ([0-9]+.?[0-9]?)\s?(<([0-9]+)>)?:(.*)(\s+.+\s+)/i',$str, $match);我試過這段代碼,這得到了這樣的內容:$match[5][0]: 內容章節 1 第 1 行$match[5][1]:內容第二章第二行問題在: (\s+.+\s+)。我怎樣才能獲得全行內容?!非常感謝。
查看完整描述

1 回答

?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

目前您只匹配一行。

您可以使用重復模式來匹配所有不以 #chapter 開頭的行,使用負前瞻:

#Chapter\h+\d+:\h+title\h+\d+\K(?:\R(?!#chapter).*)*

解釋

  • #Chapter 字面匹配

  • \h+\d+: 匹配 1+ 個水平空白字符、1+ 個數字和 :

  • \h+title\h+\d+匹配 匹配 1+ 個水平空白字符,title匹配 1+ 個水平空白字符和 1+ 個數字

  • \K 忘記匹配的內容

  • (?: 非捕獲組

    • \R(?!#chapter).* 匹配unicode換行序列并斷言直接在右邊的不是#chapter

  • )* 關閉捕獲組并重復 0+ 次

例如:


preg_match_all('/#Chapter\h+\d+:\h+title\h+\d+\K(?:\R(?!#chapter).*)*/i',$str, $match);

print_r($match[0]);

結果


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



)


查看完整回答
反對 回復 2021-08-28
  • 1 回答
  • 0 關注
  • 141 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號