簡體   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