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正则表达式在替换中使用Match作为Dict键_Python_Regex_Perl_Python 3.x - Fatal编程技术网

Python正则表达式在替换中使用Match作为Dict键

Python正则表达式在替换中使用Match作为Dict键,python,regex,perl,python-3.x,Python,Regex,Perl,Python 3.x,我正在将一个程序从Perl翻译成Python(3.3)。我对Python相当陌生。在Perl中,我可以进行巧妙的正则表达式替换,例如: $string =~ s/<(\w+)>/$params->{$1}/g; 如果我可以传递一个将正则表达式匹配映射到dict的函数,那可能会更好。这可能吗 谢谢您的帮助。您可以将一个可调用的消息传递给re.sub,告诉它如何处理匹配对象 s = re.sub(r'<(\w+)>', lambda m: replacement_di

我正在将一个程序从Perl翻译成Python(3.3)。我对Python相当陌生。在Perl中,我可以进行巧妙的正则表达式替换,例如:

$string =~ s/<(\w+)>/$params->{$1}/g;
如果我可以传递一个将正则表达式匹配映射到dict的函数,那可能会更好。这可能吗


谢谢您的帮助。

您可以将一个可调用的消息传递给
re.sub
,告诉它如何处理匹配对象

s = re.sub(r'<(\w+)>', lambda m: replacement_dict.get(m.group()), s)
我要注意的是,当使用
re.sub
(和family,即
re.split
)时,当指定存在于所需替换周围的内容时,使用lookaround表达式通常更为简洁,这样匹配周围的内容就不会被分包出去。所以在这种情况下,我会像这样写你的正则表达式

r'(?<=<)(\w+)(?=>)'

很好,这解决了我的问题。谢谢你的详细解释。
lambda m: replacement_dict.get(m.group(), m.group()) 
# fallback to just leaving the word there if we don't have a replacement
r'(?<=<)(\w+)(?=>)'
s = "<sometag>this is stuff<othertag>this is other stuff<closetag>"

d = {'othertag': 'blah'}

#this doesn't work because `group` returns the whole match, including non-groups
re.sub(r'<(\w+)>', lambda m: d.get(m.group(), m.group()), s)
Out[23]: '<sometag>this is stuff<othertag>this is other stuff<closetag>'

#this output isn't exactly ideal...
re.sub(r'<(\w+)>', lambda m: d.get(m.group(1), m.group(1)), s)
Out[24]: 'sometagthis is stuffblahthis is other stuffclosetag'

#this works, but is ugly and hard to maintain
re.sub(r'<(\w+)>', lambda m: '<{}>'.format(d.get(m.group(1), m.group(1))), s)
Out[26]: '<sometag>this is stuff<blah>this is other stuff<closetag>'

#lookbehind/lookahead makes this nicer.
re.sub(r'(?<=<)(\w+)(?=>)', lambda m: d.get(m.group(), m.group()), s)
Out[27]: '<sometag>this is stuff<blah>this is other stuff<closetag>'