2

我有一个遵循这种可预测模式的字符串:

This is a string, it has a comma in it, This is another string, it also has a comma in it, This is a third string, it follows the trend

等等。

显然字符串代表一个列表,列表用逗号隔开。只是,列表中的项目也有逗号。确定新项目开头的方法是大写字母。

我设法将模式与此匹配:[, \p{Lu}]但我不确定下一步该做什么。如果我使用preg_split()我会丢失逗号,这是需要的,但我也会丢失大写字母,而不是。正确的 preg_replaced 字符串应如下所示

This is a string, it has a comma in it<br />
This is another string, it also has a comma in it<br />
This is a third string, it follows the trend
4

1 回答 1

11

使用前瞻断言

$result = preg_replace('/, (?=\p{Lu})/u', '<br />\n', $subject);

正则表达式的意思是“匹配一个,和一个空格,但前提是它们后跟一个大写的 Unicode 字母”。这样,这封信就不会成为匹配本身的一部分。

于 2013-05-29T16:25:52.963 回答