多行模式和标签搜索
我正在尝试为标签制作一个模式,但 sub 方法只是替换行末尾的第一个字符和 3,我试图用多行替换该行上的所有标签
p=re.compile('<img=([^}]*)>([^}]*)</img>', re.S)
p.sub(r'[img=\1]\2[/img]','<img="test">dsad</img> <img="test2">dsad2</img>')
output:
'**[**img="test">dsad</img> <img="test2"]dsad2**[/img]**'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用 re 模式的开头:
这将吞噬(作为第 1 组)前导
之后的所有字符,包括其他标签!!!,直到最后一个
>
它可能会吞噬;*
是贪婪的——它会尽可能多地吞噬。不确定为什么要专门排除闭大括号}
?也许您的意思是排除闭尖括号(>
)。对于非贪婪匹配,您需要
*?
,而不是*
;这样,你就会尽可能少地吞食,而不是尽可能多地吞食。所以,我认为你的意思是:这与一个
img
标签(以及其中的所有标签)匹配,并且似乎完全执行了你所说的替换。You're using towards the start of your re's pattern:
this will gobble up (as group 1) all characters after the leading
<img=
, including other tags!!!, up to the last>
it can possibly gobble;*
is GREEDY -- it gobbles up as much as it possibly can. Not sure why you're specifically excluding closed-braces}
? Maybe you meant to exclude closed angular brackets instead (>
).For NON-greedy matching, instead of
*
, you need*?
; with that, you'll be gobbling up as little as you can, instead of as much as you can. So, I think you mean:this matches one
img
tag (and all tags inside it), and appears to be performing exactly the substitutions you mean.