Skip to main content
 首页 » 编程设计

python之查找并替换为以小写字母开头的正确句子案例句子。正则表达式或崇高

2024年11月24日28insus

我有一些句子以小写字母开头的文本。我需要找到它们并用正确的句子大小写替换。有些标点符号不正确。即句号从句号开始,没有空格。

即

.this sentence 
and this.also this. and this.This one is not. 

替换为->

.This sentence 
And this.Also this. And this.This one is not. 

sublime text 3 解决方案,regex 或 python nltk 解决方案是合适的。

我试过这个解决方案。但它很慢,并且找不到句号后没有空格的句子。

import nltk.data 
from nltk.tokenize import sent_tokenize 
text = """kjdshkjhf. this sentence 
and this.also this. and this. This one is not.""" 
 
aa=sent_tokenize(text) 
for a in aa: 
    if (a[0].islower()): 
        print a 
        print "****" 

请您参考如下方法:

你可以使用这个模式

^([^a-zA-Z]*)([a-z]) 

并使用$1\U$2作为替代

Regex Demo

更新:- 如果您想在每个 .(句点)之后捕获第一个小写字母,您可以使用此

^([^a-zA-Z]*)([a-z])|(\.\s*)([a-z]) 

Demo