我想替换字符串中匹配的重新模式的文本,并且可以使用re.sub()
执行此操作。如果我在调用中传递一个函数作为repl
参数,它可以根据需要工作,如下所示:
from __future__ import print_function
import re
pattern = r'(?P<text>.*?)(?:<(?P<tag>\w+)>(?P<content>.*)</(?P=tag)>|$)'
my_str = "Here's some <first>sample stuff</first> in the " \
"<second>middle</second> of some other text."
def replace(m):
return ''.join(map(lambda v: v if v else '',
map(m.group, ('text', 'content'))))
cleaned = re.sub(pattern, replace, my_str)
print('cleaned: {!r}'.format(cleaned))
输出:
cleaned: "Here's some sample stuff in the middle of some other text."
但是从文档来看,听起来我应该能够通过传递一个替换字符串来获得相同的结果,该字符串中包含对其中命名组的引用。然而,我这样做的尝试不起作用,因为有时一个组是不匹配的,并且为它返回的值是None
(而不是空字符串''
)。
cleaned = re.sub(pattern, r'\g<text>\g<content>', my_str)
print('cleaned: {!r}'.format(cleaned))
输出:
Traceback (most recent call last):
File "test_resub.py", line 21, in <module>
cleaned = re.sub(pattern, r'\g<text>\g<content>', my_str)
File "C:\Python\lib\re.py", line 151, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "C:\Python\lib\re.py", line 278, in filter
return sre_parse.expand_template(template, match)
File "C:\Python\lib\sre_parse.py", line 802, in expand_template
raise error, "unmatched group"
sre_constants.error: unmatched group
我做错了什么或不理解?
def repl(matchobj):
if matchobj.group(3):
return matchobj.group(1)+matchobj.group(3)
else:
return matchobj.group(1)
my_str = "Here's some <first>sample stuff</first> in the " \
"<second>middle</second> of some other text."
pattern = r'(?P<text>.*?)(?:<(?P<tag>\w+)>(?P<content>.*)</(?P=tag)>|$)'
print re.sub(pattern, repl, my_str)
您可以使用re.sub
的调用函数。
编辑:cleaned = re.sub(pattern, r'\g<text>\g<content>', my_str)
这不会起作用,因为最后一点字符串匹配,即of some other text.
有\g<text>
定义,但没有\g<content>
,因为没有content.But你仍然要求re.sub
这样做。所以它生成错误。如果你使用字符串"Here's some <first>sample stuff</first> in the <second>middle</second>"
然后你的print re.sub(pattern,r"\g<text>\g<content>", my_str)
将工作,因为\g<content>
一直在这里定义。
如果我理解正确,你想删除< >
之间的所有内容:
>>> import re
>>> my_str = "Here's some <first>sample stuff</first> in the <second>middle</second> of some other text."
>>> print re.sub(r'<.*?>', '', my_str)
Here's some sample stuff in the middle of some other text.
有点解释这里发生了什么... r'<.*?>'
:
<
找到第一个<
然后.
接受任何角色
*
接受任何字符的任何次数
?
将结果限制在尽可能短的时间,没有这个,它会一直持续到最后一个>
而不是第一个可用的
>
找到了关闭点>
然后,在没有任何东西的情况下替换这两点之间