You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将字符串中两个及以上连续英文句点替换为unicode省略号?

连续英文句点替换规则及实现

需求说明

我们需要实现的字符串替换规则如下:

  • 仅保留单个英文句点的原有样式,不做任何修改
  • 所有出现2个及以上连续英文句点的位置,统一替换为Unicode省略号(…)

替换效果示例

  • Hey. → Hey.(单个句点无变化)
  • Hey.. → Hey…
  • Hey... → Hey…
  • Hey.... → Hey…
  • Hey..... → Hey…(所有2个及以上连续句点统一替换为Unicode省略号)

实现方案

核心通过正则匹配连续2个及以上的英文句点,统一替换即可,匹配规则为\.{2,},以下是常见编程语言的实现示例:

  • Python
import re

def replace_continuous_dots(text: str) -> str:
    return re.sub(r'\.{2,}', '…', text)
  • JavaScript
function replaceContinuousDots(text) {
    return text.replace(/\.{2,}/g, '…');
}
  • Java
import java.util.regex.Pattern;

public class DotReplaceUtil {
    private static final Pattern CONTINUOUS_DOT_PATTERN = Pattern.compile("\\.{2,}");

    public static String replace(String text) {
        return CONTINUOUS_DOT_PATTERN.matcher(text).replaceAll("…");
    }
}

内容的提问来源于stack exchange,提问作者Mark Yoon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 23:54:03