Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用正则表达式从文件名中列出多个可能的字符串_Python_Regex - Fatal编程技术网

Python 使用正则表达式从文件名中列出多个可能的字符串

Python 使用正则表达式从文件名中列出多个可能的字符串,python,regex,Python,Regex,我想根据多个引用在文件夹中列出几个PNG。因此,在列表中,我希望PNG的名称中包含字符串“7029113”或“7031503”。这就是我到目前为止得到的,我只需要知道如何使用正则表达式,可能我的通配符也错了,我不确定 render_path = "C:/BatchRender/Renaming" os.chdir(render_path) list_files = glob.glob("*.png") r = re.compile(".*7029113.*" OR ".*7031503.*")

我想根据多个引用在文件夹中列出几个PNG。因此,在列表中,我希望PNG的名称中包含字符串“7029113”或“7031503”。这就是我到目前为止得到的,我只需要知道如何使用正则表达式,可能我的通配符也错了,我不确定

render_path = "C:/BatchRender/Renaming"
os.chdir(render_path)
list_files = glob.glob("*.png")

r = re.compile(".*7029113.*" OR ".*7031503.*")
list_40 = list(filter(r.match, list_files))  

这是一种方法

r = re.compile('.*(7029113|7031503).*')

“(“*7029113.*.*7031503.””
。这可能可以通过从组中删除公共部分(
*70
3.*
)来缩短。如果使用
re.search
而不是
re.match
,则不需要
*
前缀和后缀;仅对子字符串进行匹配就足以生成一个真值。如果不希望只在字符串开头搜索匹配项,请不要使用
re.match
。使用
重新搜索
。而且模式是基本的:
word1 | word2
谢谢!你介意解释一下“*”吗?我知道它是一个通配符,但我不明白为什么它不仅仅是点。
通常用于匹配“任何字符”。请阅读以下文档以了解有关regex的更多信息<代码>https://docs.python.org/3/howto/regex.html#matching-字符