python - Python re.split 和 re.findall:分组和捕获
问题描述
我有类似"00:00:00 Segment 1 00:20:00 Segment 2 8:00:00 Segment 3"
and的字符串,"00:00 Segment 1 20:0 Segment 2"
并且想要使用re.split()
和re.findall()
查找所有时间戳和段名称。但是我无法在没有捕获效果的情况下实现可选组。这是我得到的:
str_1 = "00:00:00 Segment 1 00:20:00 Segment 2 8:00:00 Segment 3"
str_2 = "00:00 Segment 1 20:0 Segment 2"
re.findall(r'\d\d?:\d\d?:\d\d?', str_1)
=> ['00:00:00', '00:20:00', '8:00:00']
re.split(r'\d\d?:\d\d?:\d\d?', str_1)
=> ['', ' Segment 1 ', ' Segment 2 ', ' Segment 3']
以上工作正常,但将无法处理str_2
。如果我制作了第三对数字,它只返回可选组
re.findall(r'\d\d?:\d\d?(:\d\d?)?', str_1)
=> [':00', ':00', ':00']
re.split(r'\d\d?:\d\d?(:\d\d?)?', str_1)
=> ['', ':00', ' Segment 1 ', ':00', ' Segment 2 ', ':00', ' Segment 3']
re.findall(r'\d\d?:\d\d?(:\d\d?)?', str_2)
=> ['', '']
re.split(r'\d\d?:\d\d?(:\d\d?)?', str_2)
=> ['', None, ' Segment 1 ', None, ' Segment 2']
但是,如果我在没有捕获的情况下创建了可选组,则str_2
工作正常,但结果与str_1
re.findall(r'\d\d?:\d\d?(?:\d\d?)?', str_1)
=> ['00:00', '00:20', '8:00']
re.split(r'\d\d?:\d\d?(?:\d\d?)?', str_1)
=> ['', ':00 Segment 1 ', ':00 Segment 2 ', ':00 Segment 3']
re.findall(r'\d\d?:\d\d?(?:\d\d?)?', str_2)
=> ['00:00', '20:0']
re.split(r'\d\d?:\d\d?(?:\d\d?)?', str_2)
=> ['', ' Segment 1 ', ' Segment 2']
我想找到一个在str_
和上都可以正常工作的正则表达式str_2
,有点具有可选组但没有捕获效果。无论如何要做到这一点?
解决方案
看起来:
您的模式中缺少 ;您需要两个,一个用于 the ?:
,另一个用于您的文字:
,ala:
re.findall(r'\d\d?:\d\d?(?::\d\d?)?', str_1)
=> ['00:00:00', '00:20:00', '8:00:00']
推荐阅读
- javascript - 过滤已过滤的表或一次使用多个过滤器
- javascript - Jest 和 React 测试库未在页面加载时加载最终 DOM
- synchronization - 尽管使用了 sync 和 eval 方法,但 ArrayFire 数组计算的数据在执行过程中有所不同
- python-3.x - 在特定索引中扩展列表
- wordpress - Apache HTTPS 重定向不适用于深层链接
- laravel - CloudBuild 中 AppEngine 的 Laravel 迁移
- kotlin - 为什么函数中的类不能序列化?
- python - 如何在 Linux 上从 2 切换到 Python 3
- python - 将 Flask 应用程序部署到 Google Cloud Platform 时无法获得正确的 TensorFlow 版本
- java - 是否有在 Quarkus 中使用 InheritableThreadLocal 的等效方法?