【问题标题】:RegEx to exclude directory, capture filename that are separated by commas, exclude "(number)" and extensionsRegEx 排除目录,捕获以逗号分隔的文件名,排除“(数字)”和扩展名
【发布时间】:2016-04-30 21:58:54
【问题描述】:

过去三天(是的)我一直在尝试制作一个图像/短视频标记系统供我自己使用,但事实证明这对我来说是一个挑战。

这些是字符串:

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"

【问题讨论】:

标签: python regex regex-negation regex-lookarounds


【解决方案1】:

获取基本名称,将括号中的整数和扩展名替换为空字符串并去掉空格。

from ntpath import basename
import re
map(str.strip, re.sub('\(\d+\)|\.\w+$', '', basename(s)).split(','))

演示:

>>> s = 'd:\images\tagging 1\GIFs\kung fu panda, fight.webm'
>>> map(str.strip, re.sub('\(\d+\)|\.\w+$', '', basename(s)).split(','))
['kung fu panda', 'fight']
>>> s = 'd:\images\tagging 1\GIFs\kung fu panda, fight (2).webm'
>>> map(str.strip, re.sub('\(\d+\)|\.\w+$', '', basename(s)).split(','))
['kung fu panda', 'fight']
>>> s = 'd:\images\tagging 1\GIFs\kung fu panda 2, fight.webm'
>>> map(str.strip, re.sub('\(\d+\)|\.\w+$', '', basename(s)).split(','))
['kung fu panda 2', 'fight']
>>> s = 'd:\images\tagging 1\GIFs\kung fu panda 2, fight (2).webm'
>>> map(str.strip, re.sub('\(\d+\)|\.\w+$', '', basename(s)).split(','))
['kung fu panda 2', 'fight']
>>> s = 'd:\images\tagging 1\GIFs\pulp fiction, samuel l. jackson, angry, funny.webm'
>>> map(str.strip, re.sub('\(\d+\)|\.\w+$', '', basename(s)).split(','))
['pulp fiction', 'samuel l. jackson', 'angry', 'funny']

【讨论】:

    【解决方案2】:

    您的问题不是很清楚,但我认为您想解析文件名。如果是这种情况,我不建议您使用 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'
    

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

    【讨论】:

    • 请注意,在我的 linux 机器上,这不起作用。 ntpath.basename 可能是更可靠的选择...取决于问题是什么:)
    • @timgeb,感谢您的建议。它也不适用于我的 Linux 机器。但它应该在 OP 的机器上工作,假设它是一个 Windows 机器,我认为考虑到输入是合理的。我在我的代码 sn-p 中添加了一条评论,提到了 ntpath,因为这是一个有用的补充。
    【解决方案3】:

    如果我得到你,你想要在1\GIFs\ 之后的最后一个标签(功夫熊猫,fight.webm)——如果你添加更多内容字符串,那么我可以为你规范化代码。 这段代码只是提取标签并生成一个常规列表。 重新导入

    s="""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"""
    
    lines = s.split('\n')# Just generate a list of lines
    for t in lines:
        data = re.search(r'1\\GIFs\\(.+$)',t)
        print data.group(1).split(',')
    

    输出-

    ['kung fu panda', ' fight.webm']
    ['kung fu panda', ' fight (2).webm']
    ['kung fu panda 2', ' fight.webm']
    ['kung fu panda 2', ' fight (2).webm']
    ['pulp fiction', ' samuel l. jackson', ' angry', ' funny.webm']
    

    表达式1\\GIFs\\(.+$) 将捕获1\\GIFs 之后的最后一个标签

    LIVE-DEMO

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 2016-03-24
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多