2024-10-06 13:22:42 发布
网友
下面是我的代码,我只想得到值265,我不想要255 MT和233
import re string1 = "start news, having 255 MT, 233 and 265" price_find = re.findall(r'^\d{3}\s[A-Z]{2}|\d{3}', string1) print(price_find)
如果我运行这个,我会得到255和265
['255', '233', '265']
但我试图得到如下输出:
['233', '265']
只需使用以下正则表达式:
price_find = re.findall(r'\d{3}(?!\sMT)', string1)
它看起来不像是一个正则表达式问题。收集所有数字并使用索引:
import re string1 = "start news, having 255 MT, 233 and 265" price_find = re.findall(r'\d+', string1) print(price_find[0]) # first, 255 print(price_find[-1]) # last, 265 print(price_find[1]) # second, 233
见Python proof
只需使用以下正则表达式:
它看起来不像是一个正则表达式问题。收集所有数字并使用索引:
见Python proof
相关问题 更多 >
编程相关推荐