【问题标题】:Python regex: to calculate on matched numbers and substitute with resultPython regex:计算匹配的数字并用结果替换
【发布时间】:2019-05-28 15:57:48
【问题描述】:

我想匹配以下内容:

- [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>\n\n'
Output = re.sub(Pattern, Replace, Output)
print('Output')

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

【问题讨论】:

    标签: regex python-3.x substitution arithmetic-expressions calculation


    【解决方案1】:

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

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

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

    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)\n\n <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>\n\n'.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 demo

    输出:

    - [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>
    

    【讨论】:

      猜你喜欢
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 2020-09-01
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      相关资源
      最近更新 更多