2

以简单的方式使用 php 正则表达式,是否可以修改字符串以在单词后面的句点之后添加空格,但不能在前后跟数字(例如 1.00)的句点之后添加空格?我还需要它来忽略单字母缩写,例如 NY

String.Looks like this.With an amount of 1.00 and references N.Y.

需要改成...

String. Looks like this. With an amount of 1.00 and references N.Y.

这当然应该允许字符串中的多个实例......

4

2 回答 2

5
$text = preg_replace('/(\.)([[:alpha:]]{2,})/', '$1 $2', $text);
于 2012-08-26T03:25:15.923 回答
1

您可以使用此正则表达式:

/((?<=[A-Za-z0-9])\.(?=[A-Za-z]{2})|(?<=[A-Za-z]{2})\.(?=[A-Za-z0-9]))/

替换字符串是'. '.

请注意,上面的正则表达式在句号的一侧至少需要一个字母字符.,而在另一侧则需要字母数字字符。

测试字符串:

"String.Looks like this.With an amount of 1.00 and references N.Y.Mr.NoOne.30% replaced.no 50.Don't know."

输出:

"String. Looks like this. With an amount of 1.00 and references N.Y. Mr. NoOne. 30% replaced. no 50. Don't know."
于 2012-08-26T03:31:04.843 回答