【发布时间】:2010-04-26 11:34:24
【问题描述】:
Python 中 re 模块的一个特殊怪癖(否则非常强大)是 re.split() will never split a string on a zero-length match,例如,如果我想沿单词边界拆分字符串:
>>> re.split(r"\s+|\b", "Split along words, preserve punctuation!")
['Split', 'along', 'words,', 'preserve', 'punctuation!']
而不是
['', 'Split', 'along', 'words', ',', 'preserve', 'punctuation', '!']
为什么会有这个限制?是设计使然吗?其他正则表达式风格是否也有这样的行为?
【问题讨论】:
-
re-module 从 3.7 开始支持零长度匹配。