我做这种手术有问题,比如说我们有一根绳子
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
有regex的解决方案吗?
如果我想储存525585和559呢?
你可以试试这个:
它会给你:
您可以使用regex组来实现这一点。下面是一些示例代码:
groups
然后包含数字。此语法使用正则表达式组。使用^{} 模块:
\d
是任何数字[0-9]*
表示从0到无限次()
表示要捕获的内容+
表示从1到无限次如果需要将生成的字符串转换为} :
int
s,请使用^{或者
list comprehension:
相关问题 更多 >
编程相关推荐