![](/img/trans.png)
[英]How to remove everything (except certain characters) after the last number in a string
[英]Remove everything after specified number of characters
我有一個文件包含類似的東西:
2021-01-01Y24:19:20.2150821B Claim created under policy: P60003331
2021-12-21T19:09:01.3159955Z I-PACE
2022-11-21X11:09:00.1159335X 2000000000
2021-02-01X21:11:01.7150331A 1020621033
我想以某種方式解析它以獲得這個:
P60003331
2000000000
1020621033
怎么做? 我嘗試使用正則表達式但沒有解決方案。
你可以試試這個可能有用:
data = value.split(' ')
if any(char.isdigit() for char in data[-1]):
print(data[-1])
假設邏輯是只打印那些包含任何數字的標記,那么你可以這樣做:
import re
data = ['2021-01-01Y24:19:20.2150821B Claim created under policy: P60003331',
'2021-12-21T19:09:01.3159955Z I-PACE',
'2022-11-21X11:09:00.1159335X 2000000000',
'2021-02-01X21:11:01.7150331A 1020621033']
for line in data:
t = line.split()[-1]
if (re.findall(r'[\d]+', t)):
print(t)
Output:
P60003331
2000000000
1020621033
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.