[英]Python: regex elements match with List
我有一个列表,我想将列表的每个元素与 regex 列表进行比较,然后仅打印 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
我可以一一打印正则表达式,但如何将正则表达式与列表进行比较。
我将使用fnmatch模块而不是使用re模块,因为它看起来像通配符模式匹配。
请查看此链接以获取有关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
如果有帮助,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.