python:英文间保留一个空格,中文间空格删除
python代码:
import re def remove_space_between_cn_en(text): strArray = re.split(' ',text) if len(strArray) < 2: return text result = '' for t in strArray: if t=='': continue if re.search('[a-zA-Z0-9]$',result) and re.search('^[a-zA-Z0-9]',t): result = result + ' ' + t else: if not result=='': result = result + t else: result = t return result s = "你 好 Hello World 世 界" print(remove_space_between_cn_en(s))
输出结果:
你好Hello World世界