我想知道一种简单的方法(例如,使用正则表达式)来完成以下文本处理任务:假设我有一个像“abc123def sd123sc p123dd apple”这样的句子。我想做这些:
所以上面的例子会给出这个结果:“PabcdefS PsdscS PpddS apple”。
如何以最简单的方式执行此操作,例如在文本编辑器中使用正则表达式?我使用的是 Linux,所以任何使用命令行的方法对我也有好处。
你可以用python来做
def getWords(sentence):
words=sentence.strip().split()
words123=[]
for word in words:
if '123' in word:
words123.append("P"+word.replace('123','')+"S")
else:
words123.append(word)
result=' '.join(words123)
return result
f=open("c:\\temp\\file.txt","r",encoding='utf8')
sentence="1"
while sentence:
sentence=f.readline()
print(getWords(sentence))
f.close()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句