【发布时间】:2020-04-17 10:44:54
【问题描述】:
我正在尝试获取文本块中每行最后一个括号内的数字。但是,我目前的方法只获取最后一个括号的数字。
我试过了(数字可以是任何数字):
import re
items = """
PTT KBALTHNAL (07) PETROL STATION (500003985)
ZHONGGUO (035) SHAXIAN 01 (5001039)
AHARATHAN BAIJIAING (0837) SU YOUMING (500086)
"""
ids = re.findall(r"\((\d+)\)$",items)
print(ids)
我得到的输出:
['500086']
我追求的输出:
['500003985','5001039','500086']
如何从每行的最后一个括号中提取数字?
【问题讨论】:
-
re.findall(r"\((\d+)\)$",items, re.M)。或者不要使用re.M而使用(?:$|\n) -
re.findall(r"\((\d+)\)\n",items) -
不需要接受错误的答案,它只会伤害SO。
标签: python regex python-3.x python-re