我想从转换字符串(字幕):
585
00:59:59,237 --> 01:00:01,105
- It's all right. - He saw us!
586
01:00:01,139 --> 01:00:03,408
I heard you the first time.
进入
59:59-没关系。 -他看见了我们!
01:00:01我第一次听到你的声音。
*我想要的是:如果时间在一个小时内,则修剪掉“ 00:”前缀,而如果时间大于1小时则保留它*
我的正则表达式是:
pat = re.compile(r"""
#\s* # Skip leading whitespace
\d+\s # remoe lines contain only numbers
((?:(?:00)|(?P<hour>01)):(?P<time>\d{2}:\d{2})[,0-9->]+.*)[\r\n]+(?P<content>.*)[\r\n]+
""",
re.VERBOSE)
data = pat.sub(r"\g<hour>\g<time> \g<content>", data)
仅在不使用'
\g<hour>
'时才有效。谁能帮我?
最佳答案
我认为,这就是您要寻找的:
import re
s = """
585
00:59:59,237 --> 01:00:01,105
- It's all right. - He saw us!
586
01:00:01,139 --> 01:00:03,408
I heard you the first time.
"""
for line in re.findall(r'(\d+:)(\d+:\d+)(?:.*\n)(.*)', s):
if line[0] == '00:':
print ' '.join(line[1:])
else:
print ' '.join([''.join(line[0:2]), line[2]])
输出:
# 59:59 - It's all right. - He saw us!
# 01:00:01 I heard you the first time.
关于python - python regex sub%H:%M:%S到%M:%s或01:%M:%s,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16759896/