gpt4 book ai didi

php - 将段落中的所有点替换为新行,但数字之间和 Dr.、Mr.、Bsc 之后的点除外。 .... ETC

转载 作者:可可西里 更新时间:2023-11-01 00:59:12 26 4
gpt4 key购买 nike

将段落中的所有点替换为新行,但数字之间和 Dr.、Mr.、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

我尝试了以下但这个正则表达式替换了所有点。

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

最佳答案

尝试

$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

关于php - 将段落中的所有点替换为新行,但数字之间和 Dr.、Mr.、Bsc 之后的点除外。 .... ETC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32175699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com