简体   繁体   English

正则表达式匹配目录,然后匹配子目录

[英]Regex match directories and then match subdirectories

For example, if I have a list of paths (ie. dir1/subdirA, dir2/subdirB, dir1/subdirB, etc..). 例如,如果我有一个路径列表(即dir1 / subdirA,dir2 / subdirB,dir1 / subdirB等)。 I have a regex to match some directory names and then another regex to match the subdirectories. 我有一个正则表达式来匹配一些目录名,然后再一个正则表达式来匹配子目录。 What's the best way to get the valid paths. 获取有效路径的最佳方法是什么。 Or is there a way to combine the two regex using the 2 existing regex? 还是有一种方法可以使用现有的2个正则表达式来组合两个正则表达式?

DIR_RE = re.compile(r'somedirname', re.I)

SUB_RE = re.compile(r'^/somesubdir$', re.I)
import re

directories = ["dir1/subdirA", "dir2/subdirB", "dir1/subdirB", "subdir9/dirC"]

expression = re.compile('^dir[1-9]\/subdir[A-Z]$', re.I)

for directory in directories:
    if (re.match(expression, directory)):
        print "Yes the directory path :" +directory+ "  is valid"
        #Do something.
        #Passed cases = ["dir1/subdirA", "dir2/subdirB", "dir1/subdirB"]
    else:
        #Failed cases = ["subdir9/dirC"]
        #Do something here.

NOTE : The regex is created keeping in mind the example provided in case your directory structure is different, you have to change it accordingly. 注意:创建正则表达式时要牢记所提供的示例,以防目录结构不同,您必须相应地对其进行更改。

You can combine both regexps this way. 您可以通过这种方式组合两个正则表达式。 This example is an alternative based on solution proposed by @ZdaR. 此示例是基于@ZdaR提出的解决方案的替代方法。

import re
directories = ["dir1/subdirA", "dir2/subdirB", "dir1/subdirB", "subdir9/dirC"]

regexp = re.compile('^(dir[1-9])\/(subdir[A-Z])$', re.I)

for path in directories:
    frag = regexp.match(path)
    if frag != None:
        dir_str = frag.group(1)
        subdir_str = frag.group(2)
        entire_match = frag.group(0)
        # Do something with them

Notice the parenthesis in the regexp. 注意正则表达式中的括号。 These parenthesis allow to define several groups inside the regexp, so fragments of every match can be obtained using group(n) method over the Match object. 这些括号允许在正则表达式内定义几个组,因此可以使用Match对象上的group(n)方法获得每个匹配项的片段。

This regexp assumes that the length of every path will be only 2 此正则表达式假定每个路径的长度只有2

(Eg: 'dir/subdir' and not 'dir/subdir/subdir') (例如:“ dir / subdir”而不是“ dir / subdir / subdir”)

I hope this will help. 我希望这将有所帮助。

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

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