简体   繁体   中英

Multi-line Pattern and tag search

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]**'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM