正则表达式匹配 Python 中设置的 rar 存档文件中的第一个文件 [英] Regex to match the first file in a rar archive file set in Python

查看:51
本文介绍了正则表达式匹配 Python 中设置的 rar 存档文件中的第一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解压缩目录中的所有文件,为此我需要找到集合中的第一个文件.我目前正在使用一堆 if 语句和循环来执行此操作.我可以使用正则表达式来做到这一点吗?

I need to uncompress all the files in a directory and for this I need to find the first file in the set. I'm currently doing this using a bunch of if statements and loops. Can i do this this using regex?

这是我需要匹配的文件列表:

Here's a list of files that i need to match:

yes.rar
yes.part1.rar
yes.part01.rar
yes.part001.rar
yes.r01
yes.r001

这些不应该匹配:

no.part2.rar
no.part02.rar
no.part002.rar
no.part011.rar
no.r002
no.r02

我在 this 线程上发现了一个类似的正则表达式,但似乎 Python 不支持可变长度环视.单行正则表达式会很复杂,但我会很好地记录下来,这不是问题.这只是您解决的问题之一.

I found a similar regex on this thread but it seems that Python doesn't support varible length lookarounds. A single line regex would be complicated but I'll document it well and it's not a problem. It's just one of those problems you beat your heap up, over.

提前谢谢各位.

:)

推荐答案

没有必要为此使用后视断言.由于您从字符串的开头开始查看,因此您可以使用前瞻来完成所有事情,而使用后视则可以.这应该有效:

There's no need to use look behind assertions for this. Since you start looking from the beginning of the string, you can do everything with look-aheads that you can with look-behinds. This should work:

^((?!\.part(?!0*1\.rar$)\d+\.rar$).)*\.(?:rar|r?0*1)$

要按照您的要求捕获文件名的第一部分,您可以这样做:

To capture the first part of the filename as you requested, you could do this:

^((?:(?!\.part\d+\.rar$).)*)\.(?:(?:part0*1\.)?rar|r?0*1)$

这篇关于正则表达式匹配 Python 中设置的 rar 存档文件中的第一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆