用新线替换段落中的所有点,除了数字之间的点和Dr.博士先生,Bsc之后。 ......等

时间:2015-08-24 06:27:12

标签: php regex

用新行替换段落中的所有点,除了数字之间的点和Dr.博士先生,Bsc之后。 ......等等。

例如:

考虑这个段落

My name is Ayman. I'm 31 years. I'm 1.92M. I have BSc. degree in Computer Engineering

我想应用此类REGEX并将其转换为以下内容:

My name is Ayman.
I'm 31 years.
I'm 1.92M.   <===== note the '.' between 1 and 92 did not replace with new line
I have BSc. degree in Computer Engineering  <=== the same . after BSc did not replace with new line

我尝试了下面这个但是这个REGEX取代了所有的点。

$desc['contents']=preg_split("/(?<!\..)([\?\!\.]+)\s(?!.\.)/",$desc['contents'],-1, PREG_SPLIT_DELIM_CAPTURE);

3 个答案:

答案 0 :(得分:2)

您可以使用此正则表达式进行搜索:

(?:BSc|[JSMD]r|Mr?s|\d)\.(*SKIP)(*F)|(\.\h*)

并替换为"$1\n"

RegEx Demo

$str = preg_replace('/(?:BSc|[JSMD]r|Mr?s|\d)\.(*SKIP)(*F)|(\.\h*)/i', '$1\n', $str);

您可以在DOT之前在(?:BSc|[JSMD]r|Mr?s|\d)中添加更多要忽略的字模式。

(*SKIP)(*F)一起提供了一个很好的限制替代方案,你不能在上面的正则表达式中拥有可变长度的lookbehind。

答案 1 :(得分:2)

尝试

$str = "My name is Ayman. I'm 31 years. I'm 1.92M. I have BSc. degree in Computer Engineering";

$str = preg_split("/([\?\!\.]+)(?=\s+[A-Z])/",$str);

foreach($str as $new_str)
{
    echo $new_str.".<br />";
}

输出

My name is Ayman.
I'm 31 years.
I'm 1.92M.
I have BSc. degree in Computer Engineering.

Demo

答案 2 :(得分:0)

我认为你可以像这样使用捕获组:

/\.\d|BSc\.|Mrs?\.|Dr\.|([.!?])/

将所有替换\1.\n替换

请注意,我认为您需要在.之类的数字之前忽略.1而不是And counter is 30.之后的数字

相关问题