多行模式和标签搜索

发布于 2024-08-04 17:24:04 字数 329 浏览 1 评论 0 原文

我正在尝试为标签制作一个模式,但 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]**'

I'm trying to make a pattern for tags, but the sub method just replaces the first char and 3 at the end of the line, im trying to replace all tags on the line and with multiline

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2024-08-11 17:24:04

您正在使用 re 模式的开头:

<img=([^}]*)>

这将吞噬(作为第 1 组)前导 之后的所有字符,包括其他标签!!!,直到最后一个 > 它可能会吞噬; * 是贪婪的——它会尽可能多地吞噬。不确定为什么要专门排除闭大括号 }?也许您的意思是排除闭尖括号(>)。

对于非贪婪匹配,您需要 *?,而不是 *;这样,你就会尽可能少地吞食,而不是尽可能多地吞食。所以,我认为你的意思是:

p = re.compile(r'<img=([^>]*?)>(.*?)</img>', re.S)

这与一个 img 标签(以及其中的所有标签)匹配,并且似乎完全执行了你所说的替换。

You're using towards the start of your re's pattern:

<img=([^}]*)>

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:

p = re.compile(r'<img=([^>]*?)>(.*?)</img>', re.S)

this matches one img tag (and all tags inside it), and appears to be performing exactly the substitutions you mean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文