这可能是一个简单的问题:) 我尝试将以下内容转换为:
<gallery>File:ReDescribe.jpg|Photo by:J. K.File:redescribe_still1.pngFile:redescribe_still2.jpegFile:redescribe_still3.jpgFile:redescribe_still4.jpgFile:redescribe_still5.jpg</gallery>
进入:
[[File:ReDescribe.jpg|photo by: J K]][[File:redescribe_still1.png]] [[File:redescribe_still2.jpeg]] [[File:redescribe_still3.jpg]] [[File:redescribe_still4.jpg]] [[File:redescribe_still5.jpg]]
首先,我正在寻找一个只能选择每个 File:filename.ext 的 Python 正则表达式
到目前为止我想到了'File:(.*?)File'
但此表达式排除了最后一个 File: 因为它后面没有任何字符。
查看 regex_tester https://regex101.com/r/iV1mD9/1
表达式怎么可能也匹配最后一个 File: 后面跟着 </gallery>
?
最佳答案
File:(.*?)(?=File:|<\/gallery>)
试试这个。查看演示。使用 lookahead
确保最后一个 File:
也被捕获。
关于Python 正则表达式困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620660/