python - 在字符串中查找最长的数字子序列
问题描述
我有一个类似的字符串:“Aggecvehtafv12357748615ahfwgbej134”我想在python中找到该字符串中最长的数字子序列。我想要的答案是:“12357748615”
解决方案
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'
推荐阅读
- amazon-web-services - AWS HTTPS Certification not working more than 48 hours dns update
- reactjs - React.js 材料表:如何在加载时禁用操作图标?
- javascript - 如何在 reactjs 中创建像 google-input-tool 这样的下拉输入?
- apache-pig - 如何在猪中将字符串转换为元组
- spring - Atomikos - 唯一的资源名称
- r - 如何在 (...) 中获取值并取平均值?
- linux - 检测 USB 驱动器何时连接脚本未按预期工作
- git - 为什么在将本地分支推送到远程 git repo 后远程分支被跟踪但本地分支没有上游分支?
- intellij-idea - 如何在 Intellij 中删除检查配置文件?
- jquery - 当动态添加的文本区域为空时禁用提交按钮 - jQuery