我可以用python中几个正则表达式的并集来扩展单个字符串吗?

我可以用python中几个正则表达式的并集来扩展单个字符串吗?,python,regex,Python,Regex,我正在攻击一个转换文件类型的方法,允许用户指定转换(一个python函数)和一个正则表达式来更改文件名 在一个例子中,我有一系列正则表达式和一个输出字符串,我想用我所有正则表达式组的并集来扩展它们: import re re_strings = ['(.*).txt', '(.*).ogg', 'another(?P<name>.*)'] regexes = map(re.compile, re_strings] input_files = ['cats.txt', 'music.o

我正在攻击一个转换文件类型的方法,允许用户指定转换(一个python函数)和一个正则表达式来更改文件名

在一个例子中,我有一系列正则表达式和一个输出字符串,我想用我所有正则表达式组的并集来扩展它们:

import re
re_strings = ['(.*).txt', '(.*).ogg', 'another(?P<name>.*)']
regexes = map(re.compile, re_strings]
input_files = ['cats.txt', 'music.ogg', 'anotherpilgrim.xls']
matches = [regexes[i].match(input_files[i]) for i in range(len(regexes))]

outputstr = 'Text file about: \1, audio file about: \2, and another file on \g<name>.'
# should be 'Text file about: cats, audio file about: music, and another file on pilgrim.xls'
但这会强制re匹配整个文件,而不仅仅是文件名的某一部分。我可以在文件a la
(.*?
之间放入一个catch-all组,但这肯定会打乱组引用,并可能打乱原始模式(我无法控制)。我想我也可以在任何地方强制命名组,然后合并所有regex.groupdict()s

Python不允许部分扩展,因此所有组引用都必须有效,因此不可能对groupdict进行一系列扩展,例如:

for m in matches:
    outputstr = m.expand(outputstr)

谢谢你的建议

下面是如何组合多个正则表达式结果,并对它们进行替换的记录

给定多个查询字符串和多个正则表达式匹配:

import re

query_str = ["abcdyyy", "hijkzzz"]
re_pattern = [r"(a)(b)(?P<first_name>c)(d)",
              r"(h)(i)(?P<second_name>j)(k)"]

# match each separately
matches= [re.search(p,q) for p,q in 
          zip(re_pattern, query_str)]

为什么不使用
'文本文件about:{0[0]},音频文件about:{1[0]},以及另一个文件about:{2[0]}..format(*(m.groups()表示匹配项))
来代替呢?脱离主题,但您可以让您的迭代更简洁一些:
[r.match(f)表示zip中的(f,r)(input_文件,regex)]
更清晰一些,我无法控制正则表达式的内容,也无法控制
outputstr
的内容@拉尔克斯,好消息@MartijnPieters,使用格式匹配而不是重新扩展可能会达到目的。。。将测试。基于您的想法@MartijnPieters,我还可以包括命名组:
outputstr.format(*{I:m.group(I)表示范围内的I(m.lastindex)+m.groupdict().keys()}表示匹配中的m])
。填写正确答案,并将其标记为已接受。
import re

query_str = ["abcdyyy", "hijkzzz"]
re_pattern = [r"(a)(b)(?P<first_name>c)(d)",
              r"(h)(i)(?P<second_name>j)(k)"]

# match each separately
matches= [re.search(p,q) for p,q in 
          zip(re_pattern, query_str)]
replacement = r"[\4_\g<first_name>_\2_\1:\5:\6:\8:\g<second_name>]"
import sre_parse

#
#   dummy object to provide group() function and "string" member variable
#
class match_substitute:
    def __init__(self, matches): 
        self.data = []
        for m in matches:
            self.data.extend(m.groups())
        self.string = ""
    # regular expression groups count from 1 not from zero!
    def group(self, ii):
        return self.data[ii - 1]



#
#   dummy object to provide groupindex dictionary for named groups
#
class pattern_substitute:
    def __init__(self, matches, re_pattern): 
        #
        #   Named group support
        #   Increment indices so they are point to the correct position
        #       in the merged list of matching groups
        #
        self.groupindex = dict()
        offset = 0
        for p, m in zip(re_pattern, matches):
            for k,v in sre_parse.parse(p).pattern.groupdict.iteritems():
                self.groupindex[k] = v + offset
            offset += len(m.groups())



match   = match_substitute(matches)
pattern = pattern_substitute(matches, re_pattern)

#
#   parse and substitute
#
template = sre_parse.parse_template(replacement, pattern)
result = sre_parse.expand_template(template, match)