首页 > 解决方案 > 在字符串中查找最长的数字子序列

问题描述

我有一个类似的字符串:“Aggecvehtafv12357748615ahfwgbej134”我想在python中找到该字符串中最长的数字子序列。我想要的答案是:“12357748615”

标签: pythonpython-3.xstring

解决方案


Short solution: use re.findall to find the sequences of digits, and get the longest by using len as the key for max:

import re

s = 'Aggecvehtafv12357748615ahfwgbej134'

print(max(re.findall(r'\d+', s), key=len))
# '12357748615'

推荐阅读