Skip to main content
 首页 » 编程设计

Python 正则表达式困难

2024年10月25日29over140

这可能很简单:) 我尝试转换以下内容:

<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'但是这个表达式排除了最后一个文件:因为它后面没有任何字符。 看吧 regex_tester https://regex101.com/r/iV1mD9/1

表达式怎么可能还匹配最后一个文件:后跟 </gallery> ?

请您参考如下方法:

File:(.*?)(?=File:|<\/gallery>) 

试试这个。查看演示。使用 lookahead 确保最后一个 File: 也被捕获。

https://regex101.com/r/sJ9gM7/94#python