你也可以使用负前瞻...那些在超级强大的 Python 正则表达式机制中被遗忘的东西...
您可以制作一个正则表达式,以逗号分隔,逗号前面没有数字或后面没有数字。
#!/usr/bin/env python
import re
samples=[
"Foo Bar, FooTown, $100,00",
"$100,00, Foo Bar, FooTown",
"Foo Bar, $100,00, FooTown",
"$100,00, Foo Bar, FooTown,",
]
myRegex=re.compile(",(?!\d)|(?<!\d),")
for sample in samples:
print "%s sample splitted: %s (%s items)" % (sample, myRegex.split(sample), len(myRegex.split(sample)))
输出:
Foo Bar, FooTown, $100,00 sample splitted: ['Foo Bar', ' FooTown', ' $100,00'] (3 items)
$100,00, Foo Bar, FooTown sample splitted: ['$100,00', ' Foo Bar', ' FooTown'] (3 items)
Foo Bar, $100,00, FooTown sample splitted: ['Foo Bar', ' $100,00', ' FooTown'] (3 items)
$100,00, Foo Bar, FooTown, sample splitted: ['$100,00', ' Foo Bar', ' FooTown', ''] (4 items)
我为在 Python 中开发re模块的人感到非常抱歉......我见过这种很少使用的前瞻。