Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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 使用3个选项重新模式匹配_Python_Regex_Re - Fatal编程技术网

Python 使用3个选项重新模式匹配

Python 使用3个选项重新模式匹配,python,regex,re,Python,Regex,Re,这是当前的代码,我正在尝试用另一种方法: 目前我们有: URL_PREFIX = "http://ourrepo:8081/artifactory" pattern = re.compile(r'^.*-(ngwebui|nodeservice).*$') if pattern.match(artifact): return URL_PREFIX + "/npm-local/region/%s/-/region/%s-%s" % (artifact

这是当前的代码,我正在尝试用另一种方法:

目前我们有:

URL_PREFIX = "http://ourrepo:8081/artifactory"
pattern = re.compile(r'^.*-(ngwebui|nodeservice).*$')
if pattern.match(artifact):
    return URL_PREFIX + "/npm-local/region/%s/-/region/%s-%s" % (artifact, artifact, version)
else:
    return URL_PREFIX + "/libs-releases-local/org/region/%s/%s/%s-%s" % (artifact, version, artifact, version)
我想做的是将另一种称为“dockerservice”的类型与URL_前缀值结合在一起

URL_PREFIX + "/docker-dev-local/%s-%s" % (artifact, artifact, version)

如果ngwebui | nodeservice URL|前缀、dockerservice URL|前缀、else URL|前缀:,用
保留最后一个全面覆盖的最简单方法是什么?

直接的方法是使用第二个条件:

URL\u前缀=”http://ourrepo:8081/artifactory"
nodePattern=re.compile(r'^.-(ngwebui | nodeservice.*$)
dockerPattern=re.compile(r'patternForDocker')
如果节点模式匹配(工件):
返回URL_PREFIX+“/npm local/region/%s/-/region/%s-%s”%(工件,工件,版本)
elif dockerPattern.match(工件):
返回URL_前缀+“docker/path/…”
其他:
返回URL_PREFIX+“/libs releases local/org/region/%s/%s/%s-%s”%(工件,版本,工件,版本)
但更具可扩展性的方法是创建模式和路径的地图:

URL\u前缀=”http://ourrepo:8081/artifactory"
路径={
“^.*-(ngwebui | nodeservice.*$”:“/npm local/region/%s/-/region/%s-%s%”(工件,工件,版本),
“^patternForDocker$”:“docker/path/…”,
#其他双
}
对于模式,路径中的路径。项()
编译=重新编译(模式)
如果编译了.match(工件):
返回URL\u前缀+路径
返回URL_PREFIX+“/libs releases local/org/region/%s/%s/%s-%s”%(工件,版本,工件,版本)

添加带有适当条件的
elif
。在这种情况下,在if语句之外定义模式是不够的。请创建另一个模式。您没有抓住要点。