我有一根绳子:
string = 'i-phone is popular - many people like it (user-friendly system, fast, good support)'如何使用正则表达式将其拆分为:
split_string = ['i-phone', 'is', 'popular', 'many', 'people', 'like', 'it', 'user-friendly', 'system', 'fast', 'good', 'support']问题是-包含两个空格和一个连字符。
我试过: split_string = re.split('() - ',字符串),但是我得到了:
['i-phone', 'is', 'popular', '-', 'many', 'people', 'like', 'it', '', 'user-friendly', 'system,', 'fast,', 'good', 'support', '']谢谢。
发布于 2018-06-08 05:34:06
尝试使用这个正则表达式来拆分:
\s+(?:[()-]\s*)?|[,()]\s*解释
\s+ -匹配1+空白(?:[()-]\s*)? -匹配(、)、-和0+出现的空格.最后的?使这个子部分可选| -或[,()]\s* -匹配,、(或)后面的0+空格https://stackoverflow.com/questions/50753804
复制相似问题