我正在攻击转换文件类型的package,允许用户指定转换(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'
我希望用正则表达式的并集扩展outputstr
(对于\2
引用,连接可能更有意义吗?)。我可以连接re,用一些未使用的字符将它们分开:
final_re = re.compile('\n'.join(re_strings))
final_files = '\n'.join(input_files)
match = final_re.search(final_files)
但这会强制re与整个文件匹配,而不仅仅是文件名的某些部分。我可以在文件la (.*?)
之间放入一个全能组,但这肯定会弄乱组引用,它可能会搞乱原始模式(我无法控制)。我想我也可以在任何地方强制命名组,然后联合所有正则表达式.groupdict()s ...
Python不允许部分扩展,因此所有组引用都必须有效,因此无论如何都不可能对groupdict进行一系列扩展:
for m in matches:
outputstr = m.expand(outputstr)
感谢您的任何建议!
答案 0 :(得分:0)
仅供记录,以下是如何组合几个正则表达式结果的结果,并在它们之间进行替换。
给定几个查询字符串和几个正则表达式匹配:
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)