匹配 *所有 * newline型字符序列的方法
我正在寻找一种获取字符串中发现的所有类似新线的字符序列的方法。我正在尝试使用PREG_MATCH(),如下所示:
preg_match('/^[^\r\n]*(?:([\r\n]+)|[^\r\n]*)+$/', $input_text, $matches);
但是我似乎只获得了 last 这样的匹配。我觉得解决方案可能涉及\ g的使用,但是当我尝试引入它时,比赛完全失败了。我认为我不明白如何正确使用它或应该去的地方。
我意识到我的模式将以序列匹配多个新线(即空白线导致单个匹配中的多个新线)。这就是我想要的。
例如,对于字符串:
"ABC\nDEF\r\n\rGHI\n\n\r\n",
我想得到:
[ "\n", "\r\n\r", "\n\n\r\n" ]
感谢您的任何帮助。
I'm looking for a way to obtain all sequences of newline-like characters found in a string. I'm trying to use preg_match() as follows:
preg_match('/^[^\r\n]*(?:([\r\n]+)|[^\r\n]*)+$/', $input_text, $matches);
But I only appear to be getting the last such match. I feel like the solution probably involves the use of \G, but when I attempt to introduce it, the match fails entirely. I don't think I'm understanding how to use it correctly or where it should go.
I realize my pattern will match multiple newlines in sequence (i.e. blank lines lead to multiple newlines in a single match). This is what I want.
For example, for the string:
"ABC\nDEF\r\n\rGHI\n\n\r\n",
I would like to get:
[ "\n", "\r\n\r", "\n\n\r\n" ]
Thanks for any assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
它返回所有行结束序列,因为
\ r+
匹配一个或多个线结束序列。Use
It returns all line ending sequences because
\R+
matches one or more line ending sequences.