为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

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 关注
  • 88 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信