regex元素与List匹配



我有一个列表,我想比较列表的每个元素与regex列表,然后只打印regex找不到的内容。正则表达式来自配置文件:

exclude_reg_list= qa*,bar.*,qu*x

代码:

import re
read_config1 = open("config.ini", "r")
for line1 in read_config1:
    if re.match("exclude_reg_list", line1):
         exc_reg_list = re.split("= |,", line1)
         l = exc_reg_list.pop(0)
         for item in exc_reg_list:
             print item

我可以一个接一个地打印正则表达式,但是如何将正则表达式与列表进行比较。

不使用re模块,我将使用fnmatch模块,因为它看起来像通配符模式匹配。

关于fnmatch的更多信息请点击此链接。

为期望的输出扩展代码:

import fnmatch
exc_reg_list = []
#List of words for checking
check_word_list = ["qart","bar.txt","quit","quest","qudx"]
read_config1 = open("config.ini", "r")
for line1 in read_config1:
    if re.match("exclude_reg_list", line1):
        exc_reg_list = re.split("= |,", line1)
        #exclude_reg_list= qa*,bar.*,qu*x
        for word in check_word_list:
            found = 0
            for regex in exc_reg_list:
               if fnmatch.fnmatch(word,regex):
                    found = 1
            if found == 0:
                   print word 
输出:

C:Users>python main.py
quit
quest

如果有帮助请告诉我。

相关内容

最新更新