假设有一段英文,其中有单词中间的字母“i”误写为“I”,请编写程序进行纠正。
import re s = "adbjsdIsdbI hdjhI Ifdjidh " a = re.sub(r'\BI\B','i',s) print(a)
这里给出Python 3.4.2代码,如果使用Python 2.7.8的话只需要修改其中的print()函数为print语句即可。
import re x = "I am a teacher,I am man, and I am 38 years old.I am not a busInessman." print(x) pattern = re.compile(r'(?:[\w])I(?:[\w])') while True: result = pattern.search(x) if result: if result.start(0) != 0: x = x[:result.start(0)+1]+'i'+x[result.end(0)-1:] else: x = x[:result.start(0)]+'i'+x[result.end(0)-1:] else: break print(x)