我有一個字符串,我試圖將其拆分為更小的字符串d+\/(任意數(shù)字后跟 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+\/或直到整個字符串/文本的末尾?期望的結(jié)果:[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條經(jīng)驗 獲得超13個贊
分裂與'~(?=\d+/)~'
。任何功能都使用 preg_split mebe。
更新:不建議使用 split 提取記錄,
因為第一個元素不是記錄,而是必須轉(zhuǎn)置的垃圾。
僅使用斷言進行拆分也很慢,就像看著油漆變干一樣。
誰將此標記為 split 的副本以獲取記錄,誰就錯了!
如果按照這個邏輯,成千上萬的問題都是分裂的重復(fù),
所以管理員編輯他們的數(shù)據(jù)庫的時間,或者我們是無緣無故的選擇?
推薦/首選/明智的方法是將所有
記錄與此嚴格匹配(?s)\d+/.*?(?=\d+/|$)
演示包含對正則表達式的 funcshun 的評論。
- 1 回答
- 0 關(guān)注
- 153 瀏覽
添加回答
舉報
0/150
提交
取消