python - 用于排除目录、捕获以逗号分隔的文件名、排除 "(number)"和扩展名的正则表达式

标签 python regex regex-negation regex-lookarounds

在过去的三天里(是的)我一直在尝试制作一个供我自己使用的图像/短视频标签系统,但这已经证明是一个超出我能力范围的挑战。

这些是字符串:

d:\images\tagging 1\GIFs\kung fu panda, fight.webm
d:\images\tagging 1\GIFs\kung fu panda, fight (2).webm
d:\images\tagging 1\GIFs\kung fu panda 2, fight.webm
d:\images\tagging 1\GIFs\kung fu panda 2, fight (2).webm
d:\images\tagging 1\GIFs\pulp fiction, samuel l. jackson, angry, funny.webm

我尝试修改四件事来实现我想要的目标,但没有成功:

(?<=d:\\images\\tagging\s1\\GIFs\\)([\w\s])+

([a-z0-9]\s?)+

(?<=\\)[^\\]*?(?=\..*$)

[^\\/:*?"<>|\r\n]+$

1 差不多了,但没有超出第一个逗号。

2 这几乎可以完成所有操作,但我还没有找到排除目录、(#) 和扩展名的方法。

3 摘自互联网,捕获“l”。并停在那里,整个文件名,不能像我想要的那样使用逗号,捕获(#)。

4 取自 regexbuddy(是的,我实际上是在绝望中购买的),捕获 (#) 和扩展名。

@timgeb

目的是获取不带逗号、(#) 和扩展名的文件名,因此:

"kung fu panda" "fight"
"kung fu panda" "fight"
"kung fu panda 2" "fight"
"kung fu panda 2" "fight"
"pulp fiction" "samuel l. jackson" "angry" "funny"

最佳答案

你的问题不是很清楚,但我认为你想解析文件名。如果是这种情况,我不建议使用 re 作为您的主要工具。

相反,请查看 os.path :

import os.path  # Or `import ntpath` for Windows paths on non-Windows systems

dir, file_name = os.path.split('d:\images\tagging 1\GIFs\kung fu panda, fight (2).webm')
# dir = 'd:\images\tagging 1\GIFs'
# file_name = 'kung fu panda, fight (2).webm'

root, ext = os.path.splitext(file_name)
# root = 'kung fu panda, fight (2)'
# ext = '.webm'

现在你有一个更简单的问题:删除括号中的数字。

关于python - 用于排除目录、捕获以逗号分隔的文件名、排除 "(number)"和扩展名的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34978260/

相关文章:

正则表达式匹配除 ACTION LOGDIR ="/vz/actionlog"之外的任何其他内容

python - 使用 vim、ctags 和其他工具检查源代码

ios - 在 Swift 中匹配独立的子字符串

php - 如何匹配 &lt;iframe&gt; 标签的一部分?

regex - 正则表达式的否定?

regex - 删除撇号 Regex 以外的标点符号

python - 按子列表元素匹配过滤列表

python - 在什么情况下调用父类的多个元类?

python - Scrapy 隐藏的内存泄漏

Python 逻辑运算符正则表达式