我正在尝试将一些大写 bbcode 标签替换为小写,如下所示:
p=re.compile(r'\[URL="(.*?)"\](.*?)\[/URL\]',re.S+re.I)
message=p.sub('[url=\\1]\\2[/url]',message)
但我需要替换大量标签,因此我不会为每个标签进行编译。如果我使用 [(.*?)],则 sub 无论如何都会替换为相同的大写字母。
所以问题是:
如何在 Python 中使用 RE 替换并小写 2 个或更多组
最佳答案
那么,我对这里的 1 组使用了简单的方法:
text='dsads [QUOTE]test[/QUOTE]<br><br>[URL=http://test.com]what[/URL] dsadkd [B]TEST[/B]'
def replacement(match):
return "["+match.group(1).lower()+"]"
>>> re.sub(r'\[(.*?)\]', replacement, text)
RESULT: 'dsads [quote]test[/quote]<br><br>[url=http://test.com]what[/url] dsadkd [b]TEST[/b]'
关于python - 如何在Python中使用re替换和小写2个或更多组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16929941/