繁体   English   中英

如何解析子字符串并在python中获取子字符串后的数字

[英]How to parse sub string and get the digit after the substring in python

我想获取子字符串最后一次出现的数字

input : "abc1  foo barabc2 abc3 abc4 foobar"
output : 4

您可以使用re.findall

import re
s = "abc1  foo barabc2 abc3 abc4 foobar"
print(re.findall('\d+', s)[-1])

输出:

4

好吧,如果这是您唯一想要的东西,那么我根本不会使用正则regexp ,而是:

s = "abc1 foo barabc2 abc3 abc4 foobar"
print([c for c in s if c.isdigit()][-1])

我希望你正在寻找这样的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM