python - 正则表达式按数字匹配组
问题描述
import re
digits = '122223444'
预期的:
['1', '2222', '3','444']
解决方案
您可以使用捕获组和反向引用
(\d)\1*
import re
regex = r"(\d)\1*"
test_str = "122223444"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
推荐阅读
- reactjs - FCM 网络推送通知 Google Analytics
- scala - 无法从 Spark 应用程序连接到 Hive 元存储
- javascript - 如何缩短 Firebase 身份验证令牌的过期时间以进行测试
- php - PHP:单击后退按钮仍会重定向到上一页
- c++ - 如何使用 GO 语言读取可能是两种不同数据类型之一的 HDF5 属性?
- python - 无法在 google colab 上安装 functools32
- bazaar - 查看最新的开发版本
- postgresql - PostgreSQL:DISTINCT 中的最新行的性能低于 GROUP BY 中的最大行
- c# - .NET Core 依赖注入;尝试激活服务时无法解析类型“System.String”的服务
- linux - Windows MBR 被 linux 命令覆盖 dd if=/dev/zero of=/dev/sdb bs=512 count1