gpt4 book ai didi

Python re.split 和 re.findall : grouping and capturing

转载 作者:行者123 更新时间:2023-11-28 19:00:23 28 4
gpt4 key购买 nike

我有像 "00:00:00 Segment 1 00:20:00 Segment 2 8:00:00 Segment 3""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 上都能正常工作的正则表达式,有点像可选组但没有捕获效果。无论如何要实现这一目标?

最佳答案

看起来您的模式中缺少 :;你需要两个,一个用于 ?:,一个用于你的文字 :,ala:

re.findall(r'\d\d?:\d\d?(?::\d\d?)?', str_1)
=> ['00:00:00', '00:20:00', '8:00:00']

关于Python re.split 和 re.findall : grouping and capturing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53291806/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com