简体   繁体   English

如何检查字符串是否与某个模式匹配?

[英]How to check if a string matches a certain pattern?

I have a string which is basically a file path of an .mp4 file. 我有一个字符串,它基本上是.mp4文件的文件路径。

I want to test if the file path is matching one of the following patterns: 我想测试文件路径是否匹配以下模式之一:

/*.mp4   (nothing before the slash, anything after)
*/*.mp4  (anything before and after the slash)
[!A]*.mp4  (anything before the extension, **except** for the character 'A')

What would be the best way to achieve this? 实现这一目标的最佳方法是什么? Thanks! 谢谢!

EDIT: 编辑:

I'm not looking to test if the file ends with .mp4, i'm looking to test if it ends with it and matches each of those 3 scenarios separately. 我不打算测试文件是否以.mp4结尾,我想测试它是否以它结束并分别匹配这3个场景中的每一个。

I tried using the 'endswith' but it's too general and can't "get specific" like what i'm looking for in my examples. 我尝试使用'endswith',但它太笼统,不能像我在我的例子中所寻找的那样“具体”。

Here they are: 他们来了:

string.endswith('.mp4') and string.startswith('/')

string.endswith('.mp4') and "/" in string

string.endswith('.mp4') and "A" not in string

Or, look at using fnmatch . 或者,看看使用fnmatch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM