【发布时间】:2013-08-03 01:31:23
【问题描述】:
我在执行此类操作时遇到问题,假设我们有一个字符串
teststring = "This is a test of number, number: 525, number: 585, number2: 559"
我想将 525 和 585 存储到一个列表中,我该怎么做?
我以一种非常愚蠢的方式做到了,可以,但必须有更好的方法
teststring = teststring.split()
found = False
for word in teststring:
if found:
templist.append(word)
found = False
if word is "number:":
found = True
有正则表达式的解决方案吗?
跟进:如果我想存储 525、585 和 559 怎么办?
【问题讨论】:
-
为什么要在这里使用正则表达式?一个简单的列表理解会更快并且更易读
-
@tobyodavies 你需要几个步骤,比如拆分和解析数字等吗?