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

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

正則表達式無法在換行后捕獲字符串

正則表達式無法在換行后捕獲字符串

PHP
嗶嗶one 2023-05-26 16:16:15
我有一個字符串,我試圖將其拆分為更小的字符串d+\/(任意數字后跟 a /)。這是我的字符串:2/  This is a string that has some words on a new line3/ This is another string that might also have some words on a new line到目前為止,我正在使用這段代碼:$re = '/\d+\/\s+.*/';$str = '2/  This is a string that has some words on a new line3/ This is another string that might also have some words on a new line';preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);但問題是,對于第一場比賽,我只得到第一行:[Match 1] 2/  This is a string[Match 2] 3/ This is another string that might also have some words on a new line我怎樣才能讓所有的行都匹配到d+\/或直到整個字符串/文本的末尾?期望的結果:[Match 1] 2/  This is a string that has some words on a new line[Match 2] 3/ This is another string that might also have some words on a new line
查看完整描述

1 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

分裂與'~(?=\d+/)~'。任何功能都使用 preg_split mebe。

更新:不建議使用 split 提取記錄,
因為第一個元素不是記錄,而是必須轉置的垃圾。
僅使用斷言進行拆分也很慢,就像看著油漆變干一樣。
誰將此標記為 split 的副本以獲取記錄,誰就錯了!
如果按照這個邏輯,成千上萬的問題都是分裂的重復,
所以管理員編輯他們的數據庫的時間,或者我們是無緣無故的選擇?

推薦/首選/明智的方法是將所有
記錄與此嚴格匹配(?s)\d+/.*?(?=\d+/|$)

演示

演示包含對正則表達式的 funcshun 的評論。


查看完整回答
反對 回復 2023-05-26
  • 1 回答
  • 0 關注
  • 140 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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