regexp add space after period but not when period represents a decimal or letter abbreviation?

做~自己de王妃 提交于 2019-12-12 17:39:38

问题


Using php regexp in a simple way, is it possible to modify a string to add a space after periods that follow words but not after a period that is preceded and followed by a number such as 1.00? I also need it to ignore single letter abbreviations such as N.Y.

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

Needs to be changed to...

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

This should allow for multiple instances within the string of course...


回答1:


$text = preg_replace('/(\.)([[:alpha:]]{2,})/', '$1 $2', $text);



回答2:


You can use this regex:

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

And the replacement string is '. '.

Note that the regex above expects at least one alphabetical character on one side of the full stop ., and expects alphanumeric character on the other side.

Test string:

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

Output:

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


来源:https://stackoverflow.com/questions/12127372/regexp-add-space-after-period-but-not-when-period-represents-a-decimal-or-letter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!