Python 正则表达式:计算匹配的数字并用结果替换



我想匹配以下内容:

- [Michael - YouTube](https://www.youtube.com/watch?v=3nsoN-LS8RQ){1:00,1:03}

并将其转换为

- [Michael - YouTube; **Start:1m 0s; End:1m 3s**](https://www.youtube.com/watch?v=3nsoN-LS8RQ&t=60s) 
<center><iframe width="400" height="300" src="https://www.youtube.com/embed/3nsoN-LS8RQ?start=60&end=63" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></center>

我试过这个:

# Data.md
    - [Michael - YouTube](https://www.youtube.com/watch?v=3nsoN-LS8RQ){1:00,1:03}
# python3 Test.py < Data.md
import sys
import re
Output = sys.stdin.read()
Pattern = "(.*)[([^]]*YouTube)](https://www.youtube.com/watch?.*v=([^&]*)[^)]*){[ ]?*([0-9]?*)[^,]?*:[ ]?*([0-9]?*)[^,]?*[,]?[ ]?*([0-9]?*)[^,]?*:[ ]?*([0-9]?*)[^,]?*}"
tStart=int(\4) * 60 + int(\5)
tEnd=int(\6) * 60 + int(\7)
Replace = '\1[\2 **Start:\4m \5s End:\6m \7s**](https://www.youtube.com/watch?v=\3&t=',tStart,'s) <center><iframe width="400" height="300" src="https://www.youtube.com/embed/\3?start=',tStart,'&end=',tEnd,' "frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></center>nn'
Output = re.sub(Pattern, Replace, Output)
print('Output')

如何计算tStart=int(\4) * 60 + int(\5)tEnd=int(\6) * 60 + int(\7)

有点固定的正则表达式看起来像

(.*)[([^]]*YouTube)](https://www.youtube.com/watch?S*v=([^&]*)[^)]*){[ ]*([0-9]*)[^,]*:[ ]*([0-9]*)[^,]*[,]?[ ]?([0-9]?)[^,]?:[ ]*([0-9]*)[^,]*}

请参阅正则表达式演示以查看匹配结构。由于您没有给出正则表达式规范,因此足以解决当前问题:在字符串替换模式中无法使用组内容进行计算。您需要使用回调方法作为替换参数:

import re
Output = "- [Michael - YouTube](https://www.youtube.com/watch?v=3nsoN-LS8RQ){1:00,1:03}"
Pattern = "(.*)[([^]]*YouTube)](https://www.youtube.com/watch?S*v=([^&]*)[^)]*){[ ]*([0-9]*)[^,]*:[ ]*([0-9]*)[^,]*[,]?[ ]?([0-9]?)[^,]?:[ ]*([0-9]*)[^,]*}"
def ReplaceMatch(m):
    tStart = int(float((m.group(4))))*60 + int(float(m.group(5)))
    tEnd = int(float(m.group(6)))*60 + int(float(m.group(7)))
    return '{0}[{1}; **Start:{3}m {4}s End:{5}m {6}s**](https://www.youtube.com/watch?v={2}&t={7}s)nn <center><iframe width="400" height="300" src="https://www.youtube.com/embed/{2}?start={7}&end={8}" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></center>nn'.format(m.group(1), m.group(2), m.group(3), m.group(4), m.group(5), m.group(6), m.group(7),tStart,tEnd)
Output = re.sub(Pattern, ReplaceMatch, Output)
print(Output)

请参阅 Python 演示。

输出:

- [Michael - YouTube; **Start:1m 00s End:1m 03s**](https://www.youtube.com/watch?v=3nsoN-LS8RQ&t=60s)
 <center><iframe width="400" height="300" src="https://www.youtube.com/embed/3nsoN-LS8RQ?start=60&end=63" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></center>

相关内容

  • 没有找到相关文章

最新更新