用regexp导出数字

2024-06-09 20:54:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些文字:

Bounding box for object 1 "PASpersonWalking" (Xmin, Ymin) - (Xmax, Ymax) : (160, 182) - (302, 431)

我需要提取数字160182302431。你知道吗

我可以想出一些分裂等,但它似乎太长了。有没有办法用regexp来提取它?你知道吗


Tags: boxforobject数字文字xminymaxregexp
2条回答

这是一种提取括号内数字的方法:

import re
text = 'Bounding box for object 1 "PASpersonWalking" (Xmin, Ymin) - (Xmax, Ymax) : (160, 182) - (302, 431)'
print(re.findall(r'(\d{3,})', text))

# ['160', '182', '302', '431']

您可以使用re.findall

import re
s = 'Bounding box for object 1 "PASpersonWalking" (Xmin, Ymin) - (Xmax, Ymax) : (160, 182) - (302, 431)'
new_s = re.findall('\d+', s)[-4:]

输出:

['160', '182', '302', '431']

相关问题 更多 >