匹配 *所有 * newline型字符序列的方法

发布于 2025-01-29 02:36:53 字数 469 浏览 2 评论 0原文

我正在寻找一种获取字符串中发现的所有类似新线的字符序列的方法。我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2025-02-05 02:36:53

使用

preg_match_all('/\R+/u', "ABC\nDEF\r\n\rGHI\n\n\r\n", $matches);

它返回所有行结束序列,因为\ r+匹配一个或多个线结束序列。

Use

preg_match_all('/\R+/u', "ABC\nDEF\r\n\rGHI\n\n\r\n", $matches);

It returns all line ending sequences because \R+ matches one or more line ending sequences.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文