gpt4 book ai didi

python - 如何检查字符串是否匹配特定模式?

转载 作者:太空狗 更新时间:2023-10-29 18:20:55 24 4
gpt4 key购买 nike

我有一个字符串,它基本上是一个 .mp4 文件的文件路径。

我想测试文件路径是否匹配以下模式之一:

/*.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')

实现此目标的最佳方法是什么?谢谢!

编辑:

我不想测试文件是否以 .mp4 结尾,我想测试它是否以它结尾并且分别匹配这 3 种情况。

我尝试使用“endswith”,但它太笼统了,无法像我在示例中寻找的那样“具体化”。

最佳答案

他们在这里:

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

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

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

或者,看看使用 fnmatch .

关于python - 如何检查字符串是否匹配特定模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37618224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com