正则表达式期后增加空间,但不是在周期代表小数或字母的缩写?(regexp add space aft

2019-09-21 16:45发布

以简单的方式使用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.

这样做可以使课程的字符串中的多个实例?

Answer 1:

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


Answer 2:

你可以使用这个表达式:

/((?<=[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."


文章来源: regexp add space after period but not when period represents a decimal or letter abbreviation?