正则表达式无法在换行后捕获字符串

我有一个字符串,我试图将其拆分为更小的字符串d+\/(任意数字后跟 a /)。这是我的字符串:


2/  This is a string 

that has some words on a new line

3/ 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 line

3/ 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


哔哔one
浏览 117回答 1
1回答

开心每一天1111

分裂与'~(?=\d+/)~'。任何功能都使用 preg_split mebe。更新:不建议使用 split 提取记录,因为第一个元素不是记录,而是必须转置的垃圾。仅使用断言进行拆分也很慢,就像看着油漆变干一样。谁将此标记为 split 的副本以获取记录,谁就错了!如果按照这个逻辑,成千上万的问题都是分裂的重复,所以管理员编辑他们的数据库的时间,或者我们是无缘无故的选择?推荐/首选/明智的方法是将所有记录与此严格匹配(?s)\d+/.*?(?=\d+/|$)演示演示包含对正则表达式的 funcshun 的评论。
打开App,查看更多内容
随时随地看视频慕课网APP