我认为您将无法使用单个正则表达式来做到这一点。一种方法是在引号上拆分字符串,将空格分隔的正则表达式应用于结果列表的每个其他项目,然后重新加入列表。
import redef stripwhite(text): lst = text.split('"') for i, item in enumerate(lst): if not i % 2: lst[i] = re.sub("s+", "", item) return '"'.join(lst)print stripwhite('This is a string with some "text in quotes."')


