Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个匹配数字的正则表达式,我想获得最后一个匹配数字的位置。
这就是我现在得到的:
def find_last_match_pos(pattern, s): match = None for match in re.finditer(pattern, s): pass return match.start() if match else -1
谁能想到一个更蟒蛇的方式来做到这一点?
为什么不直接使用 findall?
s.rfind(re.findall(pattern, s)[-1])