以一种简单的方式使用php regexp,是否可以修改字符串以在单词后面的句点后面添加空格,而不是在前面和后面都有数字(如1.00)的句点后面?我还需要它忽略单字母缩写,如N.Y.
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.
当然,这应该允许字符串中有多个实例。。。
$text = preg_replace('/(.)([[:alpha:]]{2,})/', '$1 $2', $text);
您可以使用以下正则表达式:
/((?<=[A-Za-z0-9]).(?=[A-Za-z]{2})|(?<=[A-Za-z]{2}).(?=[A-Za-z0-9]))/
替换字符串为'. '
。
注意,上面的regex要求在句号.
的一侧至少有一个字母字符,而在另一侧则要求有字母数字字符。
测试字符串:
"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."