Python 正则表达式困难

标签 python regex

这可能是一个简单的问题:) 我尝试将以下内容转换为:

<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: 也被捕获。

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

关于Python 正则表达式困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620660/

相关文章:

python - Sklearn,网格搜索 : how to print out progress during the execution?

python - 字谜Python 3

regex - Azure/U-SQL - 正则表达式替换

python - 正则表达式将函数参数周围的 ( ) 替换为 [ ]

objective-c - 带有分隔符和可选部分的正则表达式

javascript - 检查字符串是否至少包含 5 位数字时,没有字母或特殊字符会产生意外结果

python - 正则表达式和有限深度非转义括号匹配

python - 我无法在 Windows 10 的 cmd 中或在 kali linux 的终端中运行 .py 文件

python "re"包, "raw"字符串的奇怪现象

python - 按最后一个有效项目对 df 进行子集