如何在Notepad++中给大写字母前添加逗号?
解决方案:在每个大写字母前添加逗号
方法1:使用sed命令(Linux/macOS终端)
直接用正则匹配非行首的大写字母,在其前插入逗号:
sed -E 's/(^|[^A-Z])([A-Z])/\1,\2/g' input.txt > output.txt
- 解释:正则
(^|[^A-Z])匹配行首或非大写字母的字符,([A-Z])匹配大写字母;替换时保留前面的字符,在大写字母前加逗号。 - 执行后,
output.txt里的内容就是你要的Stack,Over,Flow ,Is,The,Best。
方法2:Python脚本(跨平台)
如果需要更灵活的处理,写个简单的Python脚本:
# 读取原文件 with open("input.txt", "r", encoding="utf-8") as f: text = f.read() processed = [] for idx, char in enumerate(text): if idx == 0: processed.append(char) else: # 遇到大写字母前加逗号 if char.isupper(): processed.append("," + char) else: processed.append(char) result = "".join(processed) # 写入结果文件 with open("output.txt", "w", encoding="utf-8") as f: f.write(result)
运行脚本后,output.txt会生成目标内容。
内容的提问来源于stack exchange,提问作者Linh Son Nguyen
相关产品推荐
相关产品推荐

